Lvalue, Rvalue, Xvalue, Prvalue, Glvalue

时间:2022-02-21 22:25:46

c++11中关于什么是lvalue, 什么是rvalue, 什么是xvalue, 什么是prvalue, 什么是…

一直搞得我晕头转向的, 今天下定决心一定要把它搞定, 写了一个程序来判断lvalue, rvalue, prvalue等

 namespace test{

 template <typename T>
struct is_lvalue_reference {
const static bool value = false;
}; template <typename T>
struct is_lvalue_reference<T&> {
const static bool value = true;
}; template <typename T>
struct is_rvalue_reference {
const static bool value = false;
}; template <typename T>
struct is_rvalue_reference<T&&> {
const static bool value = true;
}; template <typename T>
struct is_xvalue {
const static bool value = (!is_lvalue_reference<T>::value) && is_rvalue_reference<T>::value;
}; template <typename T>
struct is_prvalue {
const static bool value = !(is_lvalue_reference<T>::value || is_rvalue_reference<T>::value);
}; template <typename T>
struct is_lvalue {
const static bool value = !(is_xvalue<T>::value || is_prvalue<T>::value);
}; template <typename T>
struct is_rvalue {
const static bool value = (is_xvalue<T>::value || is_prvalue<T>::value);
}; template <typename T>
struct is_glvalue{
const static bool value = (is_xvalue<T>::value || is_lvalue<T>::value);
}; }

测试验证:

 struct Foo {};
Foo funRetFoo();
Foo& funRetFooLRef();
Foo&& funRetFooRRef(); TEST(TypeTraits, isRvalue){ //base type
EXPECT_FALSE(::test::is_lvalue_reference<int>::value);
EXPECT_FALSE(::test::is_rvalue_reference<int>::value);
EXPECT_FALSE(::test::is_lvalue<int>::value);
EXPECT_FALSE(::test::is_xvalue<int>::value);
EXPECT_TRUE(::test::is_prvalue<int>::value);
EXPECT_FALSE(::test::is_glvalue<int>::value);
EXPECT_TRUE(::test::is_rvalue<int>::value); // return obj
EXPECT_FALSE(::test::is_lvalue_reference<decltype(funRetFoo())>::value);
EXPECT_FALSE(::test::is_rvalue_reference<decltype(funRetFoo())>::value);
EXPECT_FALSE(::test::is_lvalue<decltype(funRetFoo())>::value);
EXPECT_FALSE(::test::is_xvalue<decltype(funRetFoo())>::value);
EXPECT_TRUE(::test::is_prvalue<decltype(funRetFoo())>::value);
EXPECT_FALSE(::test::is_glvalue<decltype(funRetFoo())>::value);
EXPECT_TRUE(::test::is_rvalue<decltype(funRetFoo())>::value); // return ref obj
EXPECT_TRUE(::test::is_lvalue_reference<decltype(funRetFooLRef())>::value);
EXPECT_FALSE(::test::is_rvalue_reference<decltype(funRetFooLRef())>::value);
EXPECT_TRUE(::test::is_lvalue<decltype(funRetFooLRef())>::value);
EXPECT_FALSE(::test::is_xvalue<decltype(funRetFooLRef())>::value);
EXPECT_FALSE(::test::is_prvalue<decltype(funRetFooLRef())>::value);
EXPECT_TRUE(::test::is_glvalue<decltype(funRetFooLRef())>::value);
EXPECT_FALSE(::test::is_rvalue<decltype(funRetFooLRef())>::value); // return rref obj
EXPECT_FALSE(::test::is_lvalue_reference<decltype(funRetFooRRef())>::value);
EXPECT_TRUE(::test::is_rvalue_reference<decltype(funRetFooRRef())>::value);
EXPECT_FALSE(::test::is_lvalue<decltype(funRetFooRRef())>::value);
EXPECT_TRUE(::test::is_xvalue<decltype(funRetFooRRef())>::value);
EXPECT_FALSE(::test::is_prvalue<decltype(funRetFooRRef())>::value);
EXPECT_TRUE(::test::is_glvalue<decltype(funRetFooRRef())>::value);
EXPECT_TRUE(::test::is_rvalue<decltype(funRetFooRRef())>::value); int lvalue;
// 模拟=号左边
EXPECT_TRUE(::test::is_lvalue_reference<decltype(*&lvalue)>::value);
EXPECT_FALSE(::test::is_rvalue_reference<decltype(*&lvalue)>::value);
EXPECT_TRUE(::test::is_lvalue<decltype(*&lvalue)>::value);
EXPECT_FALSE(::test::is_xvalue<decltype(*&lvalue)>::value);
EXPECT_FALSE(::test::is_prvalue<decltype(*&lvalue)>::value);
EXPECT_TRUE(::test::is_glvalue<decltype(*&lvalue)>::value);
EXPECT_FALSE(::test::is_rvalue<decltype(*&lvalue)>::value); //operator++()
EXPECT_FALSE(::test::is_lvalue_reference<decltype(lvalue++)>::value);
EXPECT_FALSE(::test::is_rvalue_reference<decltype(lvalue++)>::value);
EXPECT_FALSE(::test::is_lvalue<decltype(lvalue++)>::value);
EXPECT_FALSE(::test::is_xvalue<decltype(lvalue++)>::value);
EXPECT_TRUE(::test::is_prvalue<decltype(lvalue++)>::value);
EXPECT_FALSE(::test::is_glvalue<decltype(lvalue++)>::value);
EXPECT_TRUE(::test::is_rvalue<decltype(lvalue++)>::value); //operator++(int)
EXPECT_TRUE(::test::is_lvalue_reference<decltype(++lvalue)>::value);
EXPECT_FALSE(::test::is_rvalue_reference<decltype(++lvalue)>::value);
EXPECT_TRUE(::test::is_lvalue<decltype(++lvalue)>::value);
EXPECT_FALSE(::test::is_xvalue<decltype(++lvalue)>::value);
EXPECT_FALSE(::test::is_prvalue<decltype(++lvalue)>::value);
EXPECT_TRUE(::test::is_glvalue<decltype(++lvalue)>::value);
EXPECT_FALSE(::test::is_rvalue<decltype(++lvalue)>::value); }