1

Is there some way I can output if the template parameter is an rvalue reference, reference, or constant value? That is, I'm using a universal reference and would, for debugging/learning purposes, like to see exactly which type actually matched.

template<typename T>
void write( T && f ) {
    std::cout << typeid(f).name;
}

This of course doesn't show me that it was an rvalue, lvalue, or const parameter which was passed. I would like somehow to display which parameter type was actually matched. Note that I'm specifically interested in seeing how this single function is matched.

4

1 回答 1

3

您可能想查看type_traits标题。它具有用于检测类型是否为const以及是否为reference. 您可以查看T您绑定的参考类型:

  1. 如果f是 laval-reference-type,那么也是T
  2. 如果fconst那么是T
  3. 如果f是右值引用,则T不是引用(如果至少受参数推导约束)。

所以你可以做这样的事情:

template<typename T>
void write( T && f ) {
    std::cout << "f is a " <<(std::is_const<typename std::remove_reference<T>::type>::value?"const ":"")<<(std::is_lvalue_reference<T>::value?"lvalue":"rvalue")<<" reference";
}
于 2013-05-25T10:26:23.643 回答