From: ManicQin on
Hello all,

while using boost::operators I bump into a stack overflow.
IField is a template wrapper, I cleaned it from other distractions
so for now it looks a bit vinyl, but in my regular code all
operations
are managed through visitor pattern (I emitted it here because it is
not
relevant to my problem - just a bit of background).

when wrapping a pod I want to give an easy access to all operators
so I supply another wrapper FieldOp that wraps the IField and gives
the
programmer an easy interface for operators

FieldOp operator FieldOp
FieldOp operator IField
FieldOp operator pod

When passing the next code step by step, you will get a
stack overflow problem.

Using my debugger (visual studio 9 express) I jumped into
the operator.hpp and followed the sequence of the program,
for a reason I cannot understand in the first error line
which examples a T <= T operator I move from
less_than_comparable1::operator<=(const T& x, const T& y)
to
less_than_comparable2::operator<(const U& x, const T& y)

but they are supposed to be the same type,
and AFAIK they are supposed to move into
my implementation of operator< no?

I'd love if someone could shed some light...

p.s. since http://stackoverflow.com is online you
cant really Google anything that has the phrase
"stack overflow" ...

#include "boost/lexical_cast.hpp"
#include "boost/operators.hpp"

#include <iostream>

using namespace boost;
using namespace std;

template <class T>
class IField
{
public:

typedef T FieldType;

IField(T const& newVal)
{
data = newVal;
}

T& operator =(T const& newVal)
{
data = newVal;
return (data);
}

virtual ~IField()
{}

typename FieldType Data() { return data; }
typename const FieldType Data() const{ return data; }
void Data(typename FieldType const& newVal) { data = newVal; }

private:
T data;

};

template <class T>
class FieldOp : public T ,
boost::ordered_field_operators<FieldOp<T>,
boost::ordered_field_operators<FieldOp<T>,T,
boost::ordered_field_operators<FieldOp<T>,typename T::FieldType

> > >
{
public:

FieldOp(typename T::FieldType const& val) : T(val) {}
FieldOp(T const& val) : T(val.Data()) {}
FieldOp(FieldOp<T> const& val) : T(val.Data()) {}

bool operator > (typename T::FieldType const& val)
{
return (Data() > val);
}
bool operator > (typename T const& val)
{
return (Data() > val.Data());
}
bool operator > (typename FieldOp<T> const& val)
{
return (Data() > val.Data());
}

bool operator < (typename T::FieldType const& val)
{
return Data() < val;
}
bool operator < (typename T const& val)
{
return Data() < val.Data();
}
bool operator < (typename FieldOp<T> const& val)
{
return Data() < val.Data();
}


bool operator == (typename T::FieldType const& val)
{
return (Data() == val);
}
bool operator == (typename T const& val)
{
return (Data() == val.Data());
}
bool operator == (typename FieldOp<T> const& val)
{
return (Data() == val.Data());
}
};

int main(int argc, char* argv[])
{

IField<int> cField(1);
FieldOp<IField<int>> cOpField1(10);
FieldOp<IField<int>> cOpField2(10);

cout << (cOpField2 <= cOpField1) << endl; //1
cout << (cOpField2 <= cField) << endl; //2
cout << (cOpField2 <= 10) << endl; //3

return 0;
}


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Vladimir Jovic on
ManicQin wrote:
> Hello all,
>

Hi,

How did you compile your example???
I got tons of errors, but I compiled using g++


<SNIP>

> #include "boost/lexical_cast.hpp"
> #include "boost/operators.hpp"
>
> #include <iostream>
>
> using namespace boost;
> using namespace std;
>
> template <class T>
> class IField
> {
> public:
>
> typedef T FieldType;
>
> IField(T const& newVal)
> {
> data = newVal;
> }
>
> T& operator =(T const& newVal)
> {
> data = newVal;
> return (data);
> }
>
> virtual ~IField()
> {}
>

Next 3 lines : remove typename.

> typename FieldType Data() { return data; }
> typename const FieldType Data() const{ return data; }
> void Data(typename FieldType const& newVal) { data = newVal; }
>
> private:
> T data;
>
> };
>
> template <class T>
> class FieldOp : public T ,
> boost::ordered_field_operators<FieldOp<T>,
> boost::ordered_field_operators<FieldOp<T>,T,
> boost::ordered_field_operators<FieldOp<T>,typename T::FieldType
>
> > > >



> {
> public:
>
> FieldOp(typename T::FieldType const& val) : T(val) {}
> FieldOp(T const& val) : T(val.Data()) {}
> FieldOp(FieldOp<T> const& val) : T(val.Data()) {}
>
> bool operator > (typename T::FieldType const& val)
> {

Data is not declared or defined in this scope. You should have used
T::Data(). Same goes for all other calls to Data() method in this class.

> return (Data() > val);
> }

Remove typename.

> bool operator > (typename T const& val)
> {
> return (Data() > val.Data());
> }

Remove typename.
> bool operator > (typename FieldOp<T> const& val)
> {
> return (Data() > val.Data());
> }
>
> bool operator < (typename T::FieldType const& val)
> {
> return Data() < val;
> }

Remove typename.
> bool operator < (typename T const& val)
> {
> return Data() < val.Data();
> }

Remove typename.
> bool operator < (typename FieldOp<T> const& val)
> {
> return Data() < val.Data();
> }
>
>
> bool operator == (typename T::FieldType const& val)
> {
> return (Data() == val);
> }

Remove typename.
> bool operator == (typename T const& val)
> {
> return (Data() == val.Data());
> }

Remove typename.
> bool operator == (typename FieldOp<T> const& val)
> {
> return (Data() == val.Data());
> }
> };
>
> int main(int argc, char* argv[])
> {
>
> IField<int> cField(1);

Next two lines should cause a compilation error, because ">>" is treated
as shift right, not as the template ending.

> FieldOp<IField<int>> cOpField1(10);
> FieldOp<IField<int>> cOpField2(10);
>
> cout << (cOpField2 <= cOpField1) << endl; //1
> cout << (cOpField2 <= cField) << endl; //2
> cout << (cOpField2 <= 10) << endl; //3
>
> return 0;
> }

With all errors fixed, I am getting the segmentation fault. I tried
debugging, but the backtrace killed my debugger.

The problem is probably the way you used boost::ordered_field_operators

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: ManicQin on
> How did you compile your example???
> I got tons of errors, but I compiled using g++

I copied it right from my visual studio 9 express it had no
syntax problems according to him.

> Next 3 lines : remove typename.
> > typename FieldType Data() { return data; }
> > typename const FieldType Data() const{ return data; }
> > void Data(typename FieldType const& newVal) { data = newVal; }


Yhe I removed them later.

> > private:
> > T data;
>
> > };
>
> > template <class T>
> > class FieldOp : public T ,
> > boost::ordered_field_operators<FieldOp<T>,
> > boost::ordered_field_operators<FieldOp<T>,T,
> > boost::ordered_field_operators<FieldOp<T>,typename T::FieldType
>
> > {
> > public:
>
> > FieldOp(typename T::FieldType const& val) : T(val) {}
> > FieldOp(T const& val) : T(val.Data()) {}
> > FieldOp(FieldOp<T> const& val) : T(val.Data()) {}
>
> > bool operator > (typename T::FieldType const& val)
> > {
>
> Data is not declared or defined in this scope. You should have used
> T::Data(). Same goes for all other calls to Data() method in this class.

Data is not declared? how come?
FieldOp inherits from IField...

> > return (Data() > val);
> > }
>
> Remove typename.

.... Yep sorry I over used typename, the only place that
I needed it was when OpField tries to access
T::FieldType
I just had a different problem that I though coild be fixed by adding
typename
and forgot to remove it.

> > int main(int argc, char* argv[])
> > {
>
> > IField<int> cField(1);
>
> Next two lines should cause a compilation error, because ">>" is treated
> as shift right, not as the template ending.

they fixed it in msvc 9 - i thought that newer versions of g++
knows how to "eat" it too.

>
> > FieldOp<IField<int>> cOpField1(10);
> > FieldOp<IField<int>> cOpField2(10);
>
> > cout << (cOpField2 <= cOpField1) << endl; //1
> > cout << (cOpField2 <= cField) << endl; //2
> > cout << (cOpField2 <= 10) << endl; //3
>
> > return 0;
> > }
>
> With all errors fixed, I am getting the segmentation fault. I tried
> debugging, but the backtrace killed my debugger.
>

backtrace? sorry?
> The problem is probably the way you used boost::ordered_field_operators

well... that was my question :)
can anyone spot a problem in the way I use
boost::ordered_field_operators

thanks!


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: ManicQin on
On Dec 20, 10:02 am, ManicQin <manic...(a)gmail.com> wrote:

> > The problem is probably the way you used boost::ordered_field_operators
>
> well... that was my question :)
> can anyone spot a problem in the way I use
> boost::ordered_field_operators

Ok, found the problem.

I declared my operators

bool operator < (FieldOp<T> const& val)

and I should have declared it

bool operator < (FieldOp<T> const& val) const.

the compiler had problems deducing the correct operator to use.



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Yechezkel Mett on
On Dec 17, 3:32 pm, ManicQin <manic...(a)gmail.com> wrote:
> Hello all,
>
> while using boost::operators I bump into a stack overflow.
....
> Using my debugger (visual studio 9 express) I jumped into
> the operator.hpp and followed the sequence of the program,
> for a reason I cannot understand in the first error line
> which examples a T <= T operator I move from
> less_than_comparable1::operator<=(const T& x, const T& y)
> to
> less_than_comparable2::operator<(const U& x, const T& y)
....
> #include "boost/lexical_cast.hpp"
> #include "boost/operators.hpp"
>
> #include <iostream>
>
> using namespace boost;
> using namespace std;
....
> template <class T>
> class FieldOp : public T ,
> boost::ordered_field_operators<FieldOp<T>,
> boost::ordered_field_operators<FieldOp<T>,T,
> boost::ordered_field_operators<FieldOp<T>,typename T::FieldType
> > > >
> {
> public:
>
> FieldOp(typename T::FieldType const& val) : T(val) {}
> FieldOp(T const& val) : T(val.Data()) {}
> FieldOp(FieldOp<T> const& val) : T(val.Data()) {}
>
> bool operator > (typename T::FieldType const& val)
> {
> return (Data() > val);
> }
> bool operator > (typename T const& val)
> {
> return (Data() > val.Data());
> }
> bool operator > (typename FieldOp<T> const& val)
> {
> return (Data() > val.Data());
> }
>
> bool operator < (typename T::FieldType const& val)
> {
> return Data() < val;
> }
> bool operator < (typename T const& val)
> {
> return Data() < val.Data();
> }
> bool operator < (typename FieldOp<T> const& val)
> {
> return Data() < val.Data();
> }

These functions should be const.

What's happening is that the boost defined
operator>(const FieldOp<IField<int>>&,const FieldOp<IField<int>>&)
takes your object by const reference, and then tries to call operator<
with the arguments swapped. Since your operator< isn't const it can't
be called on a const reference. Since const FieldOp<IField<int>>&
converts automatically to const IField<int>& due to a derived-to-base
conversion, you instead end up in the boost defined
operator<(const IField<int>&,const FieldOp<IField<int>>&)
which tries to call operator> with the arguments swapped. Since your
operator> isn't const it can't be called on a const reference. Since
IField<int> converts automatically to const FieldOp<IField<int>>& via
a converting constructor, you instead end up in the boost defined
operator>(const FieldOp<IField<int>>&,const FieldOp<IField<int>>&)
which is where we were before. And so on and so forth, until the stack
overflows.

Incidentally it's generally recommended to make your operators free
functions.

Yechezkel Mett


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]