What is the difference between an operator and a member function inCRTP?

P

Peng Yu

Hi,

In the following code, the 'copy' member function works. But the '='
operator does not work. Can somebody let me know why a member function
is different from an operator.

Thanks,
Peng

#include <iostream>

template <typename D>
class B {
public:
void print() {
static_cast<D*>(this)->print();
}
template <typename D1>
B<D> &operator=(const B<D1> &that) {
_x = that._x;
}
template <typename D1>
B<D> &copy(const B<D1> &that) {
_x = that._x;
return *this;
}
int _x;
};

struct D1 : public B<D1> {
void print() {
std::cout << "D1" << std::endl;
}
};

struct D2 : public B<D2> {
void print() {
std::cout << "D2" << std::endl;
}
};

int main() {
struct D1 d1;
d1.print();
struct D2 d2;
d2.print();

d1.copy(d2);
d1 = d2;//error
}
 
K

Kai-Uwe Bux

Peng said:
#include <iostream>

template <typename D>
class B {
public:
void print() {
static_cast<D*>(this)->print();
}
template <typename D1>
B<D> &operator=(const B<D1> &that) {
_x = that._x;

You probably want to return something here.
}
template <typename D1>
B<D> &copy(const B<D1> &that) {
_x = that._x;
return *this;
}
int _x;
};

struct D1 : public B<D1> {
void print() {
std::cout << "D1" << std::endl;
}
};

struct D2 : public B<D2> {
void print() {
std::cout << "D2" << std::endl;
}
};

int main() {
struct D1 d1;
d1.print();
struct D2 d2;
d2.print();

d1.copy(d2);
d1 = d2;//error
}

The following has no compilation errors:

#include <iostream>

template <typename D>
class B {
public:
void print() {
static_cast<D*>(this)->print();
}
template <typename D1>
B<D> &operator=(const B<D1> &that) {
_x = that._x;
return *this;
}
template <typename D1>
B<D> &copy(const B<D1> &that) {
_x = that._x;
return *this;
}
int _x;
};

struct D1 : public B<D1> {
using B<D1>::eek:perator=;

void print() {
std::cout << "D1" << std::endl;
}
};

struct D2 : public B<D2> {
using B<D2>::eek:perator=;

void print() {
std::cout << "D2" << std::endl;
}
};

int main() {
struct D1 d1;
d1.print();
struct D2 d2;
d2.print();

d1.copy(d2);
d1 = d2;
}

As you can see, a using directive is needed to make the operator visible.


Best

Kai-Uwe Bux
 
J

James Kanze

In the following code, the 'copy' member function works. But
the '=' operator does not work. Can somebody let me know why a
member function is different from an operator.

It's different from the assignment operator because the compiler
will implicitly declare and define a copy assignment operator if
you don't; the compiler will never implicitly declare and define
a named function.
#include <iostream>
template <typename D>
class B {
public:
void print() {
static_cast<D*>(this)->print();
}
template <typename D1>
B<D> &operator=(const B<D1> &that) {
_x = that._x;
}
template <typename D1>
B<D> &copy(const B<D1> &that) {
_x = that._x;
return *this;
}
int _x;
};

For starters: a function template is never a copy assignment
operator. In the above, you also have a non-template B<D>&
B<D>::eek:perator=( B<D> const& ) declared (and if used, defined)
by the compiler. In this case, it doesn't matter, because the
compiler defined function does exactly the same thing as your
function template, but you should be aware of it. The presence
of the operator as a function template, even a function template
which could be instantiated with the type itself, does NOT
inhibit implicit generation of the operator by the compiler. If
the operator= should do something other than what the default
operator= does, then you must also define a non-template
operator= taking a B said:
struct D1 : public B<D1> {
void print() {
std::cout << "D1" << std::endl;
}
};

Note that the compiler has also provided an operator= function
for this class, which hide the operator= function of the base
class. It's exactly as if you'd declared a:

D1& operator=( D1 const& ) ;

You can use a using declaration to "unhide" the functions in the
base class, but this will still not prevent the compiler from
generating its version as well, which will be used if you assign
a D1 to another D1. (Templates and functions brought into the
class by means of a using declaration never inhibit the
automatic generation.)

Note that as a general rule, you will want to duplicate all of
the assignment operators in the derived class, since logically,
the assignment operators in the derived class should have a
different return type than those of the base class. (Note too
that when C++ derivation is used to implement OO inheritance,
you generally don't want to support assignment. But I'm far
from sure that that is the case here.)
struct D2 : public B<D2> {
void print() {
std::cout << "D2" << std::endl;
}
};
int main() {
struct D1 d1;
d1.print();
struct D2 d2;
d2.print();
d1.copy(d2);
d1 = d2;//error

Yep, because the only assignment operator in d1 is the compiler
generated one, which takes a d1.
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top