【发布时间】:2018-03-15 13:37:15
【问题描述】:
设foo为函数:
template< typename T >
void foo( T&& a ){}
下面的foo调用将推导出T的类型:
foo( 0 ); // is T here int or int&& ?
int a = 0;
foo( a ); // is T here int or int& ?
【问题讨论】:
-
您可以尝试使用不同的类型特征,如 en.cppreference.com/w/cpp/types/is_reference 或 std::is_same.. 并使用 std::add_lvalue_reference 等构建类型,然后查看特征的值是什么
-
这是一个示例,您可以看到自己测试的方法:wandbox.org/permlink/51APJ9Pr4fsweWrF
-
这是调试类型的技巧:
template <typename T> class TD;,然后在 foo 中使用:TD<T>();。由于没有定义TD,编译器会报错,并且在错误信息中,好的编译器会显示T的类型。
标签: c++ function templates type-deduction forwarding-reference