Which compiler between Visual Studio 10 and GCC 4.5 is correct regarding operator overloading and argument-dependendent lookup?
I have the following code:
class Foo;
class Bar;
class Bar {
public:
Bar() {
}
Bar(Foo &foo) {
}
};
class Foo {
public:
Foo() {
}
Foo(Foo &foo) {
}
Foo(const Bar &bar) {
}
};
Bar operator >> (const Bar &left, const Bar &right) { return Bar(); }
Foo a;
Foo b;
Foo c = a >> b;
In Visual Studio 10, the above code compiles fine: the compiler recognizes that Bar
can be instantiated from Foo&
, and there开发者_开发问答fore it invokes the appropriate operator >>
, which then returns a Bar
instance, and the constructor Foo(const Bar &)
is appropriately invoked.
However, GCC 4.5 does not compile the above code. It outputs the following error:
error: no matching function for call to 'Foo::Foo(Foo)'
note: candidates are: Foo::Foo(const Bar&)
note: Foo::Foo(Foo&)
note: Foo::Foo()
Why does the above happen and which compiler is correct, according to the language standard?
EDIT:
Why does C++ create a temporary Foo
object as a result of c = a >> b
, since Foo(const Bar &)
exists?
This has nothing to do with overloading or argument lookup. By defining Foo(Foo&)
, you've disabled the generation of the default copy constructor Foo(const Foo&)
, which is required to initialize c
from an rvalue. Add a ctor with the Foo(const Foo&) signature and your code will run fine. I don't know why VS10 compiles this, but I will try to look up the clauses which specify why it shouldn't happen.
Here we go: §12.8(1) specifies that a non template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X& and there are no other parameters or all other parameters have default values.
§12.8(5) says, that if we don't define a copy constructor for X (in any of the above mentioned forms), the compiler defines a copy constructor in the form X(const X&).
Thus defining Foo(Foo&) defines a copy constructor, so the compiler can't implicitly define Foo(const Foo&) anymore.
This should work:
Foo c(a >> b);
This initialization syntax:
Foo c = a >> b;
Is interpreted as
Foo c(Foo(a >> b));
is copy-initialization and requires an accessible copy-constructor which accepts const Foo&
.
From section [dcl.init]
of the standard (wording from C++0x):
The form of initialization (using parentheses or =) is generally insignificant, but does matter when the initializer or the entity being initialized has a class type; see below. If the entity being initialized does not have class type, the expression-list in a parenthesized initializer shall be a single expression.
The initialization that occurs in the form
T x = a;
as well as in argument passing, function return, throwing an exception (15.1), handling an exception (15.3), and aggregate member initialization (8.5.1) is called copy-initialization. [ Note: Copy-initialization may invoke a move (12.8). — end note ]
The initialization that occurs in the forms
T x(a); T x{a};
as well as in new expressions (5.3.4), static_cast expressions (5.2.9), functional notation type conversions (5.2.3), and base and member initializers (12.6.2) is called direct-initialization.
The T x{a}
syntax is new in C++0x, all the other rules are unchanged from C++03.
Then the following rules apply (same section):
If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. The applicable constructors are enumerated (13.3.1.3), and the best one is chosen through overload resolution (13.3). The constructor so selected is called to initialize the object, with the initializer expression or expression-list as its argument(s). If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed.
Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. The temporary is a prvalue. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized; see 12.2, 12.8.
精彩评论