What is the standard conform syntax for template constructor inheritance?

随声附和 提交于 2019-12-03 12:25:40

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/25940365/what-is-the-standard-conform-syntax-for-template-constructor-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!