r282547 - Fix defaulted member functions for templated classes.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 27 15:44:57 PDT 2016


It looks like this will reject valid code, such as:

template<bool B> struct X {
  using T = typename std::conditional<B, const X&, X&&>::type;
  X();
  X(T) = default; // either copyable or moveable, depending on B
};

On Tue, Sep 27, 2016 at 3:28 PM, Richard Trieu via cfe-commits <
cfe-commits at lists.llvm.org> wrote:

> 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.


If later code is assuming that a defaulted member of a dependent class is a
specific special member function, it is wrong; we should put the fix there
instead of here.


> 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}}
> +}
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160927/d6df70f5/attachment-0001.html>


More information about the cfe-commits mailing list