From 8ff338bfd8abd9ac5d0c1d89c1b05e2c02727174 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 12 Nov 2010 03:34:06 +0000 Subject: [PATCH] When performing initialization of a copy of a temporary object, use direct-initialization (rather than copy-initialization) to initialize the temporary, allowing explicit constructors. Fixes PR8342. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@118880 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaInit.cpp | 8 +++++--- .../dcl.init/dcl.init.ref/p5-cxx03-extra-copy.cpp | 2 +- test/SemaCXX/copy-initialization.cpp | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index ffbb76d8c5..7025369b85 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -3391,14 +3391,16 @@ static ExprResult CopyObject(Sema &S, OverloadCandidateSet CandidateSet(Loc); for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); Con != ConEnd; ++Con) { - // Only consider copy constructors and constructor templates. + // Only consider copy constructors and constructor templates. Per + // C++0x [dcl.init]p16, second bullet to class types, this + // initialization is direct-initialization. CXXConstructorDecl *Constructor = 0; if ((Constructor = dyn_cast(*Con))) { // Handle copy constructors, only. if (!Constructor || Constructor->isInvalidDecl() || !Constructor->isCopyConstructor() || - !Constructor->isConvertingConstructor(/*AllowExplicit=*/false)) + !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) continue; DeclAccessPair FoundDecl @@ -3415,7 +3417,7 @@ static ExprResult CopyObject(Sema &S, Constructor = cast( ConstructorTmpl->getTemplatedDecl()); - if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/false)) + if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) continue; // FIXME: Do we need to limit this to copy-constructor-like diff --git a/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-cxx03-extra-copy.cpp b/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-cxx03-extra-copy.cpp index ae59598f69..8c654110fa 100644 --- a/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-cxx03-extra-copy.cpp +++ b/test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5-cxx03-extra-copy.cpp @@ -53,7 +53,7 @@ void g4(const X4&); void g5(const X5&); void test() { - g1(X1()); // expected-warning{{no viable constructor copying parameter of type 'X1'; C++98 requires a copy constructor when binding a reference to a temporary [-Wbind-to-temporary-copy]}} + g1(X1()); g2(X2()); // expected-warning{{C++98 requires an accessible copy constructor for class 'X2' when binding a reference to a temporary; was private [-Wbind-to-temporary-copy]}} g3(X3()); // expected-warning{{no viable constructor copying parameter of type 'X3'}} g4(X4()); diff --git a/test/SemaCXX/copy-initialization.cpp b/test/SemaCXX/copy-initialization.cpp index 641eaa9411..a28a1941ec 100644 --- a/test/SemaCXX/copy-initialization.cpp +++ b/test/SemaCXX/copy-initialization.cpp @@ -10,8 +10,8 @@ class Y : public X { }; void f(Y y, int *ip, float *fp) { X x1 = y; // expected-error{{no matching constructor for initialization of 'X'}} - X x2 = 0; // expected-error{{no viable constructor copying variable}} - X x3 = ip; // expected-error{{no viable constructor copying variable}} + X x2 = 0; + X x3 = ip; X x4 = fp; // expected-error{{no viable conversion}} X x2a(0); // expected-error{{call to constructor of 'X' is ambiguous}} X x3a(ip); -- 2.40.0