Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 274
Niklas R Avatar asked Sep 19 '14 18:09

Niklas R


People also ask

What is the use of constructor in inheritance?

In C#, when we are working with the constructor in inheritance there are two different cases arise as follows: Case 1: In this case, only derived class contains a constructor. So the objects of the derived class are instantiated by that constructor and the objects of the base class are instantiated automatically by the default constructor.

Can we inherit base class constructors in the derived class?

This article is about the inheritance concept in C++ and how we can inherit the base class’ constructors in the derived class. In C++, particularly in object-oriented programming, the most fundamental and widely used concept is that of inheritance.

Why can’t I inherit a template?

This is because inheritance is only possible with a class, and a template is not a class unless it is instantiated by passing some data type to it. We created a Base class template containing a template type variable and a member function in the code snippet.

Why default constructor must be parametrized in Java?

But if we make any constructor say parametrized constructor in order to initialize some attributes then it must write down the default constructor because it now will be no more automatically called. Note: In Java, constructor of the base class with no argument gets automatically called in the derived class constructor.


1 Answers

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;
};
like image 116
user1978011 Avatar answered Sep 30 '22 05:09

user1978011