Search code examples
c++c++11templatestype-traitsdependent-name

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?


Solution

  • 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.