Re: Compiler Generated Default Functions
kanze wrote:
The operator& is the hard one. It's defined for all objects, of
all types. The standard doesn't consider it an implicitly
defined operator; it says that the built in operator is used if
there is no user defined one. And in fact, there is no way to
define the operator to give the semantics of the built-in
operator.
How about...
class Foo {
// ...
public:
Foo* operator&() { return this; }
const Foo* operator&() const { return this; }
// ...
};
Technically, when the compiler implicitly generates a function,
it behaves like a user defined function; the compiler generated
operator=, for example, has an address, participates in overload
resolution, and introduces sequence points. The global
operator& which the compiler uses if their is not a user defined
one requires an lvalue (which a user defined function cannot),
doesn't have an address, and doesn't introduce sequence points.
So a user-defined operator& can't be completely identical to the
built-in operator&. But "no way... to give the [same] semantics" is
still a bit overstated, isn't it?
I have found exactly one legal way to tell if operator& is user-defined
or not -- take the address.
Foo* (Foo::*p)() = &(Foo::operator &);
This fails if operator& is not user-defined.
Is there any other difference, in legal code?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]