看来,要测试常量,必须测试模板参数,但要测试右值,必须测试实际参数。 (这是使用VC++ 2012.)这段代码说明了我的意思:澄清通缉。 C++ type_traits
#include <type_traits>
#include <string>
#include <iostream>
using namespace std;
template<class T>
void f(T& x) {
cout << "f() is_const<T> and is_const<decltype<x)>" << endl;
cout << is_const<T>::value << endl; // Prints 1 when arg is const
cout << is_const<decltype(x)>::value << endl; // Prints 0 when arg is const
}
template<class T>
void g(T&& x) {
cout << "g() is_const<T> and is_const<decltype<x)>" << endl;
cout << is_const<T>::value << endl; // Prints 0 when arg is const
cout << is_const<decltype(x)>::value << endl; // Prints 0 when arg is cons
cout << "g() is_rvalue_reference<T> and is_rvalue_reverence<decltype(x)>" <<endl;
cout << is_rvalue_reference<T>::value << endl; // Prints 0 when arg is rvlaue
cout << is_rvalue_reference<decltype(x)>::value << endl; // Prints 1 when arg is rvalue
}
int main()
{
const std::string str;
f(str); // const argument
cout << endl;
g(std::string("")); // rvalue argument
return 0;
}
我很努力地理解这是为什么。有人可以解释,或者指给我一篇解释它的文章吗?如果需要,我将深入研究C++ 11标准。任何人都知道有关的部分?
'const'是*型*的一部分。 “Rvalue”是一个值类别,它是*表达式*的一部分。两件不同的事情。 –