r282547 - Fix defaulted member functions for templated classes.

Richard Trieu via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 27 15:28:59 PDT 2016


Author: rtrieu
Date: Tue Sep 27 17:28:59 2016
New Revision: 282547

URL: http://llvm.org/viewvc/llvm-project?rev=282547&view=rev
Log:
Fix defaulted member functions for templated classes.

In some cases, non-special member functions were being marked as being defaulted
in templated classes.  This can cause interactions with later code that expects
the default function to be one of the specific member functions.  Fix the check
so that templated class members are checked the same way as non-templated class
members are.

Modified:
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/cxx0x-defaulted-functions.cpp

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=282547&r1=282546&r2=282547&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Tue Sep 27 17:28:59 2016
@@ -13850,12 +13850,6 @@ void Sema::SetDeclDefaulted(Decl *Dcl, S
   CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Dcl);
 
   if (MD) {
-    if (MD->getParent()->isDependentType()) {
-      MD->setDefaulted();
-      MD->setExplicitlyDefaulted();
-      return;
-    }
-
     CXXSpecialMember Member = getSpecialMember(MD);
     if (Member == CXXInvalid) {
       if (!MD->isInvalidDecl())
@@ -13866,6 +13860,8 @@ void Sema::SetDeclDefaulted(Decl *Dcl, S
     MD->setDefaulted();
     MD->setExplicitlyDefaulted();
 
+    if (MD->getParent()->isDependentType()) return;
+
     // If this definition appears within the record, do the checking when
     // the record is complete.
     const FunctionDecl *Primary = MD;

Modified: cfe/trunk/test/SemaCXX/cxx0x-defaulted-functions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx0x-defaulted-functions.cpp?rev=282547&r1=282546&r2=282547&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/cxx0x-defaulted-functions.cpp (original)
+++ cfe/trunk/test/SemaCXX/cxx0x-defaulted-functions.cpp Tue Sep 27 17:28:59 2016
@@ -208,3 +208,31 @@ int fn() {
   t = true;
 }
 }
+
+namespace templated_class {
+template <typename T>
+class X {
+    X() = default;
+    X(const X&) = default;
+    X(X&&) = default;
+    X &operator=(const X&) = default;
+    X &operator=(X&&) = default;
+    ~X() = default;
+
+    X(T) = default;  // expected-error {{only special member functions may be defaulted}}
+    void Run() = default;  // expected-error {{only special member functions may be defaulted}}
+
+  };
+  template <typename T>
+  X<T>::X() = default; // expected-error {{definition of explicitly defaulted}}
+  template <typename T>
+  X<T>::X(const X<T>&) = default; // expected-error {{definition of explicitly defaulted}}
+  template <typename T>
+  X<T>::X(X<T>&&) = default; // expected-error {{definition of explicitly defaulted}}
+  template <typename T>
+  X<T> &X<T>::operator=(const X<T>&) = default; // expected-error {{definition of explicitly defaulted}}
+  template <typename T>
+  X<T> &X<T>::operator=(X<T>&&) = default; // expected-error {{definition of explicitly defaulted}}
+  template <typename T>
+  X<T>::~X() = default; // expected-error {{definition of explicitly defaulted}}
+}




More information about the cfe-commits mailing list