我只想验证cppreference中描述的一些规则。 无论表达式是左值还是右值,都很容易检查。
#include <cassert>
#include <iostream>
template <typename T>
bool IsLValue(const char* i_expression, T&, bool i_expected = true)
{
assert(i_expected);
std::cout << "l_value : " << i_expression << std::endl;
return true;
}
template <typename T>
bool IsLValue(const char* i_expression, T&&, bool i_expected = false)
{
assert(!i_expected);
std::cout << "r_value : " << i_expression << std::endl;
return false;
}
int main()
{
int i = 10;
int* p = &i;
IsLValue("The name of a variable 'i'", i);
IsLValue("The name of a variable 'p'", p);
IsLValue("The name of a function 'main' in scope", main);
IsLValue("The name of a function 'std::cout' in scope", std::cout);
IsLValue("Built-in pre-increment '++i'", ++i);
//etc
return 0;
}
有没有办法检查表达式是xvalue还是prvalue?