Junekey Jeon
Junekey Jeon

Reputation: 1587

Inheriting constructor with a type alias in a template derived class

Please see the following code:

struct base {};

template <class T>
struct derived : T {
  using base_type = T;
  using base_type::T;
};

int main()
{
  derived<base> x;
}

GCC accepts this code, but Clang and MSVC reject it. Who is right and why?

Upvotes: 4

Views: 383

Answers (1)

Nicholas Pipitone
Nicholas Pipitone

Reputation: 4192

using base_type::T; is a declaration, and the using before it is an alias. This is a bit of an edge case in the standard, since the real question boils down to where does the T get expanded. The C++ committee was referenced here as saying that they did not intend for that syntax to be valid, so LLVM explicitly removed it. It doesn't look like there's anything in the standard preventing it, so gcc isn't incorrect in allowing the conversion. Who is 'correct' is up to you.

Upvotes: 6

Related Questions