Are class and typename equal in the template context?

9

I use:

template<class T>
Arbol<T>::Arbol(){

}

when T is going to make a class.

and use:

template<typename T>
Arbol<T>::Arbol(){

}

when T goes to be a type for example bool , char os fundamental types.

But looking at a piece of code from a program (not created by me) I saw that I used typename where I usually use class , and until now I had not noticed that. if now you ask me why, because I could not say one hundred percent why, but maybe somehow I (imagine) that the compiler differentiated when it was a type included in the language and acted accordingly.

My question is:

Is it the same to use one or the other?

Well, the two of them have now tested it and they work well but I do not know if I'm missing something, or maybe there is some subtle difference between class and typename in this context.

    
asked by Angel Angel 06.12.2015 в 09:02
source

3 answers

10

In this context, It's the same , there's no difference.

They can be used interchangeably and exist for historical reasons.

In this blog post by Stan Lippman, he explains that < a href="https://es.wikipedia.org/wiki/Bjarne_Stroustrup"> Stroustrup initially did not want to introduce a new keyword and reused class . Even the ISO-C ++ standard was the only way to declare it.

During the standardization process, it was discovered that this caused syntactic ambiguities (see in the blog) and it was decided to introduce a new keyword to solve it. this is typename . Finally we overcharge class to work as typename and thus maintain backwards compatibility.

    
answered by 06.12.2015 / 09:29
source
8

Regarding the use of typename or class to declare the parameter-type of a template, in most cases using one or another of the keywords is indifferent, but there are certain cases in which it is required to use one or another.

For example, it is mandatory to use class for the type of a template-parameter ( but this could change in C ++ 17 ) and you need to use typename to help the compiler distinguish between a type or a value (when the type is dependent).

Extract from Paper n4051 (translation is mine):

  

Allow typename in a template-parameter (template)

     

Since the introduction of template aliases, C ++ has had templates that are not template classes, in particular it now has template-parameters that are not template classes. Even so, the syntax for template-parameters still requires keyword class :

template<typename T> struct A {};
template<typename T> using B = int;

template<template<typename> class X> struct C;
C<A> ca; // bien
C<B> cb; // bien, no es una clase plantilla
template<template<typename> typename X> struct D; // error, no se puede usar typename aqui

In summary, for the vast majority of cases class or typename is indifferent, but typename is the only option to disambiguate dependent types and class is mandatory for the type of template-parameter; These are the only two cases in which both keywords are not interchangeable, but they are not the most common uses.

    
answered by 14.01.2016 в 10:07
5

At the time of declaring a template simple it is indifferent to use class or typename . Both are exactly identical. However, for certain uses we will not have so much freedom of choice.

If we need to use a type provided by the class / structure used to specialize the template we will have to redeclare the type in our template . In this case we will have to use typename :

template<typename T>
class Foo
{
    typedef typename T::type newType;
};

template<class T>
class Foo
{
    typedef typename T::type newType;
};

If one of the types of template turns out to be another template (for example a container) we will have to use class if we want the template compile:

template< template<class> class Container, class NestedType>
void func(const Container<NestedType> &value)
{
  // ...
}
    
answered by 06.12.2015 в 12:29