Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can special member functions be defaulted if they use typedefs?

Clang compiles this fine, but GCC and MSVC complain that operator= cannot be defaulted:

#include <type_traits>

template<class T>
struct S
{
    typedef typename std::enable_if<!std::is_enum<T>::value, S>::type Me;
    S &operator=(Me const &) = default;
};

int main()
{
    S<int> s1, s2;
    s1 = s2;
}

Is this code legal? If not, would it be legal if Me had been defined as typedef S Me;?

like image 425
user541686 Avatar asked Dec 11 '21 21:12

user541686


People also ask

Are Typedefs scoped?

A typedef is scoped exactly as the object declaration would have been, so it can be file scoped or local to a block or (in C++) to a namespace or class.

Should typedef be public or private?

Any typedef used in the public interface of the class should be in the public section of the class. Rest should be private . Show activity on this post. If you declare as private , you will not be able to use them outside your class.

Do you need typedef in C++?

typedef is necessary for many template metaprogramming tasks -- whenever a class is treated as a "compile-time type function", a typedef is used as a "compile-time type value" to obtain the resulting type.


Video Answer


1 Answers

Given the lack of any indications to the contrary, I'm going to answer my own question and say that, as far as I've been able to find relevant clauses in the standard, I think the code is legal and thus GCC and MSVC are complaining erroneously.

As someone pointed out above, there appears to be a bug report tracking this issue.

like image 150
user541686 Avatar answered Oct 06 '22 01:10

user541686