https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63604

            Bug ID: 63604
           Summary: [C++11] A direct-initialization of a reference should
                    use explicit conversion functions
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kariya_mitsuru at hotmail dot com

The sample code below should be compiled successfully but it causes compilation
error by gcc.

==========================================
struct T {};

struct S {
    explicit operator T() { return T(); }
};

int main()
{
    S s;
    T&& t(s);
    (void) t;
}
==========================================
cf. http://melpon.org/wandbox/permlink/LHgajpAXzqTbpYDc

An initialization of a reference in a direct-initialization context should use
an explicit conversion function that converts to a class prvalue.


The latest C++ standard (n4140) 13.3.1.6 [over.match.ref]/p.1.1 says that

The conversion functions of S and its base classes are considered.  Those
non-explicit conversion functions that are not hidden within S and yield type
“lvalue reference to cv2 T2” (when initializing an lvalue reference or an
rvalue reference to function) or “cv2 T2” or “rvalue reference to cv2 T2” (when
initializing an rvalue reference or an lvalue reference to function), where
“cv1 T” is reference-compatible (8.5.3) with “cv2 T2”, are candidate functions.
 For direct-initialization, those explicit conversion functions that are not
hidden within S and yield type “lvalue reference to cv2 T2” or “cv2 T2” or
“rvalue reference to cv2 T2”, respectively, where T2 is same type as T or can
be converted to type T with a qualification conversion (4.4), are also
candidate functions.


I think that this sample code corresponds to the case “For
direct-initialization, ...”.


Note that this sample code is compiled successfully if the conversion function
returns an rvalue reference.
(cf. http://melpon.org/wandbox/permlink/kGpALX7zvzHzi7K5)

See also BUG 48453.

Reply via email to