Search code examples
c++c++11constructorusing-statement

What is the standard conform syntax for template constructor inheritance?


GCC 4.8.1 accepts

template <typename T>
class Subclass : public Baseclass<T>
{
public:
    using typename Baseclass<T>::Baseclass;
};

but MSVC does not. On the other hand, MSVC accepts

template <typename T>
class Subclass : public Baseclass<T>
{
public:
    using typename Baseclass::Baseclass;
};

but GCC does not. Then I've seen another kind of declaration in this questions: c++11 inheriting template constructors

template <typename T>
class Subclass : public Baseclass<T>
{
public:
    using typename Baseclass::Baseclass<T>;
};

for which MSVC warns about an "obsolete declaration style" and GCC says

prog.cpp:8:24: error: ‘template<class T> class Baseclass’ used without template parameters
        using typename Baseclass::Baseclass<T>;

I thought the first example would be the standard conform syntax. Intuitively, it looks right to me.

What is the c++11 standard conform syntax?


Solution

  • The answer is a bit buried in the standard. A using declaration is defined as (7.3.3):

    using [typename] nested-name-specifier unqualified-id;
    

    The nested-name-specifier resolves after some steps into simple-template-id which is defined as

    template-name < [template-argument-list] >
    

    In short, the standard conforming syntax is

    template <typename T>
    class Subclass : public Baseclass<T>
    {
    public:
        using typename Baseclass<T>::Baseclass;
    };