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 type
s 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
?
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 .
" 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.
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 .
The
type
s in thestd::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
.
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.
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