Listing 1: Failed reuse attempts
class X
{
public:
void f();
X &operator=(X const &x)
{
if (&x != this)
{
~X(); // #1
// Error C2675: unary '~' : 'X' does not
// define this operator or a conversion
// to a type acceptable to the predefined
// operator.
X(x); // #2
// Appears to put an anonymous X instance
// copied from x on the stack.
(this->*f)(); // #3
// This compiles OK.
(this->*~X)(); // #4
// This doesn't compile, but should it?
// Error C2275: 'X' : illegal use of this
// type as an expression.
(this->*X)(x); // #5
// Same error here.
(this->*operator=)(x); // #6
// But this _does_ compile. (I know it's
// nonsensical in this context.)
(this->f)(); // #7
// This also compiles OK, but should it?
(this->~X)(); // #8
// This compiles and works correctly, but
// again, should it?
(this->operator=)(x); // #9
// This nonsensical code also compiles,
// but again, should it?
(this->X)(x); // #10
// However, this does not compile.
// Error C2273: 'function-style cast' :
// illegal as right side of '->' operator
// End result: code from copy constructor
// has to be duplicated here.
return *this;
}
}
};