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?
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.
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.
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.
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.
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;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With