Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not required to use typename for dependent types in the following case?

I have been reading about removing reference of a type, here.

It gives the following example:

#include <iostream> // std::cout
#include <type_traits> // std::is_same

template<class T1, class T2>
void print_is_same() {
  std::cout << std::is_same<T1, T2>() << '\n';
}

int main() {
  std::cout << std::boolalpha;

  print_is_same<int, int>();
  print_is_same<int, int &>();
  print_is_same<int, int &&>();

  print_is_same<int, std::remove_reference<int>::type>(); // Why not typename std::remove_reference<int>::type ?
  print_is_same<int, std::remove_reference<int &>::type>();// Why not typename std::remove_reference<int &>::type ?
  print_is_same<int, std::remove_reference<int &&>::type>();// Why not typename std::remove_reference<int &&>::type ?
}

The types in the std::remove_reference traits are dependent types.

Possible implementation

template< class T > struct remove_reference      {typedef T type;};
template< class T > struct remove_reference<T&>  {typedef T type;};
template< class T > struct remove_reference<T&&> {typedef T type;};

But why does it not use typename std::remove_reference</*TYPE*/>::type?

like image 294
LernerCpp Avatar asked Oct 29 '19 09:10

LernerCpp


People also ask

What is dependent name in C++?

A dependent name is essentially a name that depends on a template parameter. A dependent name can be a type, a non-type, or a template parameter. To express that a dependent name stands for a type or a template, you have to use the keywords typename or template .

What is the use of typename in C++?

" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.

What is dependent name?

A dependent name is a name that depends on the type or the value of a template parameter. For example: template<class T> class U : A<T> { typename T::B x; void f(A<T>& y) { *y++; } }; The dependent names in this example are the base class A<T> , the type name T::B , and the variable y .


2 Answers

The types in the std::remove_reference traits are dependent types.

No, they are not dependent names here. The template arguments have been specified explicitly as int, int& and int&&. Therefore, the types are known at this point.

On the other hand, if you use std::remove_reference with a template parameter, e.g.

template <typename T>
void foo() {
    print_is_same<int, typename std::remove_reference<T>::type>();
}

then you have to use typename to tell that std::remove_reference<T>::type is a type as your expression now depends on the template parameter T.

like image 57
songyuanyao Avatar answered Oct 05 '22 10:10

songyuanyao


In a nutshell, you need typename to ensure the compiler that

std::remove_reference<int>::type

really is a type. Lets consider some other template

template <typename T>
struct foo {
    using type = int;
};

Here foo::type is a type. But what if someone supplies a specialization along the line of

template <> struct foo<int> {
    int type;
};

Now type is not a type but an int. Now when you use foo inside a template:

template <typanem T> 
struct bar {
    using type = typename foo<T>::type;
};

You have to ensure the compiler that foo<T>::type really is a type, not something else, because only looking at bar (and the primary template foo) the compiler cannot know that.

However, in your main the std::remove_reference<int>::type does not depend on a template parameter, hence the compiler can readily check if it is a type.

like image 45
463035818_is_not_a_number Avatar answered Oct 05 '22 09:10

463035818_is_not_a_number