8

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;?

cigien
  • 57,834
  • 11
  • 73
  • 112
user541686
  • 205,094
  • 128
  • 528
  • 886
  • AFAIK, it is not legal (or at least doesn't makes sense) to use `std::enable_if` in a `typedef` like that. It is meant to be used in either a template parameter, a function parameter, or a function return value, not to define type aliases. You are trying to define a copy assignment operator, so the input parameter needs to be `S const &` unconditionally. – Remy Lebeau Dec 11 '21 at 21:39
  • A simpler example: All compilers succeed for `using Me = S;`, but GCC and MSVC fail for `using Me = std::type_identity_t>;` – Kevin Dec 11 '21 at 21:40
  • @Kevin `using` is more powerful than `typedef`, so it's not an apples to apples comparison. `using` is more powerful specifically with regard to templates. – sweenish Dec 11 '21 at 21:57
  • @sweenish Ok, then use `typedef` in my examples. You get the same behavior: All compilers succeed for `typedef S Me;` and GCC and MSVC fail for `typedef std::type_identity_t> Me;` – Kevin Dec 11 '21 at 21:59
  • 3
    @sweenish: `using` is entirely equivalent to `typedef` (except for syntax) except that it can be *immediately* preceded by `template<…>`. – Davis Herring Dec 11 '21 at 22:00
  • You can't `typedef` a template that simply. – sweenish Dec 11 '21 at 22:00
  • 2
    @sweenish We don't need to typedef a template here – Kevin Dec 11 '21 at 22:02
  • Okay, I'm all caught up now. – sweenish Dec 11 '21 at 22:04
  • @RemyLebeau: You can imagine any template that introduces a dependent type, `enable_if` was just a simple one. You can imagine `type_identity` if you wish. And in any case, my question wasn't about the wisdom of using `enable_if`, but about the legality of the syntax. – user541686 Dec 11 '21 at 22:16
  • @Kevin: Yup, thanks. `type_identity` is C++20 so this is more general, but yeah that illustrates the issue better. – user541686 Dec 11 '21 at 22:19
  • [GCC bug report](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86646). It's not confirmed, but I'm pretty sure Clang is correct here. Indirection through an alias *shouldn't* change the signature of the function here, at least. – cigien Dec 12 '21 at 01:06
  • @cigien: Ah interesting, thanks for the link. – user541686 Dec 12 '21 at 01:17

1 Answers1

1

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.

user541686
  • 205,094
  • 128
  • 528
  • 886