Title
Explicit conversion functions in direct class initialization
Status
cd2
Section
12.2.2.5 [over.match.copy]
Submitter
Jason Merrill

Created on 2009-05-13.00:00:00 last changed 179 months ago

Messages

Date: 2010-03-15.00:00:00

[Voted into WP at March, 2010 meeting.]

Date: 2009-10-15.00:00:00

Proposed resolution (October, 2009):

Change 12.2.2.5 [over.match.copy] paragraph 1 second bullet as follows:

  • When the type of the initializer expression is a class type “cv S”, the non-explicit conversion functions of S and its base classes are considered. When initializing a temporary to be bound to the first parameter of a copy constructor (11.4.5.3 [class.copy.ctor]) called with a single argument in the context of direct-initialization, explicit conversion functions are also considered. Those that are not hidden within S and yield a type whose cv-unqualified version is the same type as T or is a derived class thereof are candidate functions. Conversion functions that return “reference to X” return lvalues or rvalues, depending on the type of reference, of type X and are therefore considered to yield X for this process of selecting candidate functions.
Date: 2009-05-13.00:00:00

Consider the following example:

    struct C { };

    struct A {
       explicit operator int() const;
       explicit operator C() const;
    };

    struct B {
       int i;
       B(const A& a): i(a) { }
    };

    int main() {
       A a;
       int i = a;
       int j(a);
       C c = a;
       C c2(a);
    }

It's clear that the B constructor and the declaration of j are well-formed and the declarations of i and c are ill-formed. But what about the declaration of c2? This is supposed to work, but it doesn't under the current wording.

C c2(a) is direct-initialization of a class, so constructors are considered. The only possible candidate is the default copy constructor. So we look for a conversion from A to const C&. There is a conversion operator to C, but it is explicit and we are now performing copy-initialization of a reference temporary, so it is not a candidate, and the declaration of c2 is ill-formed.

History
Date User Action Args
2010-03-29 00:00:00adminsetmessages: + msg2724
2010-03-29 00:00:00adminsetstatus: ready -> cd2
2009-11-08 00:00:00adminsetmessages: + msg2369
2009-11-08 00:00:00adminsetstatus: open -> ready
2009-05-13 00:00:00admincreate