template question

G

Gonçalo Rodrigues

Hi all,

Sorry to bother you guys again with a template question but I'm really
trying to understand them and there's some piece of info that I'm
missing. I'm sure I'm being really dumb here so, please bear with me.
I've reduced my problem to the following:

Remark: the example below is just to highlight my problems with
templates. I know very well that there are already ways of doing what
the example tries to do (for example, with the boost library).

So, for the example. I declare the template in a header file like:

#ifndef TEMPLATESHEADER
#define TEMPLATESHEADER

namespace python {

template<typename T>
class IComparable {
public:
//Assumes operator==.
bool operator!=(const T& other) const;
};

//Methods.
template<typename T>
bool IComparable<T>::eek:perator!=(const T& other) const {
return !(*this == other);
}
}

#endif

Now, I try to use the template in the following:

#include <iostream>
#include "object.hpp"
using namespace python;

//Constructor(s).
Object::Object() {}

//Compare operators.
bool Object::eek:perator==(const Object& other) const {
return this == &other;
}

//Simple testing: later, move this into a testobject.cpp.
int main() {
//Force instantiation.
Object obj;
std::cout << "Size of Object is " << sizeof(obj) << "." <<
std::endl;
Object objA;
std::cout << "Compare equal: " << (obj == objA) << "." <<
std::endl;
std::cout << "Compare unequal: " << (obj != objA) << "." <<
std::endl;
}

For completion's sake let me paste the object.hpp header file

#ifndef OBJECTHEADER
#define OBJECTHEADER

#include "templates.hpp"

namespace python {

class Object : public IComparable<Object> {
public:
//Default constructor.
Object();

//Compare operators.
bool operator==(const Object& other) const;

};
}

#endif

When I try to compile this with the free MS .NET c++ compiler I get:

d:\Goncalo\Programming\C++\Python--\include\templates.hpp(17) : error
C2679: bin
ary '==' : no operator found which takes a right-hand operand of type
'const pyt
hon::IComparable<T>' (or there is no acceptable conversion)
with
[
T=python::Object
]
C:\Programas\Microsoft Visual C++ Toolkit
2003\include\xstring(502) : wh
ile compiling class-template member function 'bool
python::IComparable<T>::eek:pera
tor !=(const T) const'
with
[
T=python::Object
]
D:\Goncalo\Programming\C++\Python--\include\object.hpp(12) :
see referen
ce to class template instantiation 'python::IComparable<T>' being
compiled
with
[
T=python::Object
]


Huh? Can anybody enlighten me in what I am doing wrong?

With my best regards,
G. Rodrigues
 
R

Rolf Magnus

Gonçalo Rodrigues said:
Hi all,

Sorry to bother you guys again with a template question but I'm really
trying to understand them and there's some piece of info that I'm
missing. I'm sure I'm being really dumb here so, please bear with me.
I've reduced my problem to the following:

Remark: the example below is just to highlight my problems with
templates. I know very well that there are already ways of doing what
the example tries to do (for example, with the boost library).

So, for the example. I declare the template in a header file like:

#ifndef TEMPLATESHEADER
#define TEMPLATESHEADER

namespace python {

template<typename T>
class IComparable {
public:
//Assumes operator==.

It assumes IComparable said:
bool operator!=(const T& other) const;
};

//Methods.
template<typename T>
bool IComparable<T>::eek:perator!=(const T& other) const {
return !(*this == other);
}
}

#endif

Now, I try to use the template in the following:

#include <iostream>
#include "object.hpp"
using namespace python;

//Constructor(s).
Object::Object() {}

//Compare operators.
bool Object::eek:perator==(const Object& other) const {
return this == &other;
}

Here, you have Object::eek:perator==. But your IComparable operator!=
assumes an operator in IComparable itself, not in the derived class.
 
G

Gonçalo Rodrigues

Here, you have Object::eek:perator==. But your IComparable operator!=
assumes an operator in IComparable itself, not in the derived class.

In my ignorance if the signature in the template is

bool IComparable<T>::eek:perator!=(const T& other) const;

Then it assumes a reference to a T object. If I wanted a reference to
a IComparable<T> (as seems to be really what I am doing) then I would
write

bool IComparable<T>::eek:perator!=(const IComparable<T>& other) const;

But it looks that I'm wrong. So how would I write
IComparable<T>::eek:perator!= to assume the operator in the derived
class?

TIA, with my best regards,
G. Rodrigues
 
R

Rolf Magnus

Gonçalo Rodrigues said:
In my ignorance if the signature in the template is

bool IComparable<T>::eek:perator!=(const T& other) const;

Then it assumes a reference to a T object. If I wanted a reference to
a IComparable<T> (as seems to be really what I am doing) then I would
write

bool IComparable<T>::eek:perator!=(const IComparable<T>& other) const;

But it looks that I'm wrong.

The problem is the operator== that you are trying to use.

return !(*this == other);

Means the same as:

return ! this->operator==(other);

and 'this' is of type IComparable<T>*, not of type T*. Therefore the
So how would I write
IComparable<T>::eek:perator!= to assume the operator in the derived
class?

You could do a conversion:

return ! *static_cast<T*>(this) == other;
 
A

Ali Cehreli

So how would I write
IComparable<T>::eek:perator!= to assume the operator in the derived class?

Try this:

template<typename T>
bool IComparable<T>::eek:perator!=(const T& other) const {
return !(static_cast<const T&>(*this) == other);
}

I can't think of any scenario where that static_cast may cause trouble. (?)

Ali
 
G

Gonçalo Rodrigues

[text snipped]
The problem is the operator== that you are trying to use.

return !(*this == other);

Means the same as:

return ! this->operator==(other);

and 'this' is of type IComparable<T>*, not of type T*. Therefore the
compiler is searching for IComparable<T>::eek:perator== and not for
T::eek:perator==.

Ahhh... I got it now. The this is of type IComparable<T> and not of
type T which I was implicitely assuming.

Thanks a lot!

G. Rodrigues
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top