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;
?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With