Ler*_*Cpp 10 c++ templates dependent-name type-traits c++11
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 ?
}
Run Code Online (Sandbox Code Playgroud)
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;};
Run Code Online (Sandbox Code Playgroud)
But why does it not use typename std::remove_reference</*TYPE*/>::type?
son*_*yao 22
The
types in thestd::remove_referencetraits 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>();
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
really is a type. Lets consider some other template
template <typename T>
struct foo {
using type = int;
};
Run Code Online (Sandbox Code Playgroud)
Here foo::type is a type. But what if someone supplies a specialization along the line of
template <> struct foo<int> {
int type;
};
Run Code Online (Sandbox Code Playgroud)
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;
};
Run Code Online (Sandbox Code Playgroud)
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.
| 归档时间: |
|
| 查看次数: |
578 次 |
| 最近记录: |