]> granicus.if.org Git - clang/commitdiff
Per C++ [over.match.copy]p1, direct-initialization of a reference can
authorRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 2 Jul 2018 23:25:22 +0000 (23:25 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 2 Jul 2018 23:25:22 +0000 (23:25 +0000)
only invoke converting constructors of the reference's underlying type.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@336153 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaInit.cpp
test/CXX/dcl.decl/dcl.init/dcl.init.ref/p5.cpp

index b3f401e63acce039b58f21f6bd709d553bb5d30a..6e695ff6eef694b651ed35e01365350c26b55d4e 100644 (file)
@@ -4224,9 +4224,11 @@ static OverloadingResult TryRefInitWithConversionFunction(
   OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
   CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion);
 
-  // Determine whether we are allowed to call explicit constructors or
-  // explicit conversion operators.
-  bool AllowExplicit = Kind.AllowExplicit();
+  // Determine whether we are allowed to call explicit conversion operators.
+  // Note that none of [over.match.copy], [over.match.conv], nor
+  // [over.match.ref] permit an explicit constructor to be chosen when
+  // initializing a reference, not even for direct-initialization.
+  bool AllowExplicitCtors = false;
   bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding();
 
   const RecordType *T1RecordType = nullptr;
@@ -4242,7 +4244,7 @@ static OverloadingResult TryRefInitWithConversionFunction(
         continue;
 
       if (!Info.Constructor->isInvalidDecl() &&
-          Info.Constructor->isConvertingConstructor(AllowExplicit)) {
+          Info.Constructor->isConvertingConstructor(AllowExplicitCtors)) {
         if (Info.ConstructorTmpl)
           S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl,
                                          /*ExplicitArgs*/ nullptr,
index e775e8f0e3cc5299f0f592b5041de97fd27f83e2..869fc4f014938d1b4c8ab14802475a909d5cc7ea 100644 (file)
@@ -61,3 +61,12 @@ namespace test3 {
     unsigned &t9 = (a->bitY += 3); // expected-error {{non-const reference cannot bind to bit-field 'bitY'}}
   }
 }
+
+namespace explicit_ctor {
+  struct A {};
+  struct B { // expected-note 2{{candidate}}
+    explicit B(const A&);
+  };
+  A a;
+  const B &b(a); // expected-error {{no viable conversion}}
+}