这是一些示例代码:
#include <iostream>
class Foo
{
public:
explicit Foo(int x) : data(x) {};
Foo& operator++()
{
data += 1;
return *this;
}
void *get_addr()
{
return (void*)this;
}
friend Foo operator + (const Foo& lhs, const Foo& rhs);
friend std::ostream& operator << (std::ostream& os, const Foo& f);
private:
int data;
};
std::ostream& operator << (std::ostream& os, const Foo& f)
{
return (os << f.data);
}
Foo operator + (const Foo& lhs, const Foo& rhs)
{
return Foo(lhs.data + rhs.data);
}
void bar(Foo& f)
{
std::cout << "bar(l-value ref)" << std::endl;
}
void bar(const Foo& f)
{
std::cout << "bar(const l-value ref)" << std::endl;
}
void bar(Foo&& f)
{
std::cout << "bar(r-value ref)" << std::endl;
}
int main()
{
// getting the identity of the object
std::cout << Foo(5).get_addr() << std::endl; // Can write &Foo(5)
// by overloading &
// overload resolution
bar(Foo(5)); // prints r-value ref
// default copy assignment
std::cout << (Foo(78) = Foo(86)) << std::endl; // prints 86
// mutating operations
std::cout << (++Foo(5)) << std::endl; // prints 6
// more mutating operations
std::cout << (++(Foo(78) + Foo(86))) << std::endl; // prints 165
// overload resolution
bar((Foo(78) + Foo(86))); // prints r-value ref
}
表达式如Foo(5)prvalues或一般rvalues?我可以在这些表达式上调用get_addr()的事实是否意味着它们具有身份?或者,我不能应用默认& -operator(我的意思是非重载)意味着他们没有身份,因此是prvalues?
是否公平地说通过产生它的表达式产生的值的可变性与这个值分类是正交的?
解决方法:
每个表达式都是一个,只有一个:
>左值
> xvalue
> prvalue
作为左值和x值的表达式的并集统称为glvalues.
作为xvalues和prvalues的表达式的并集统称为rvalues.
因此,xvalue表达式都被称为glvalues和rvalues.
在Alf的答案中找到的方便图表正确地说明了我用上述文字描述的关系,也可以在C标准的第3.10节,版本C 11及以上版本中找到.
我上面所说的一切,我怀疑OP已经知道,只是从这个问题的标题的措辞.
琐事:
Bjarne Stroustrup invented this classification of expressions, and in
doing so perhaps saved the entire rvalue reference proposal from
collapsing in the Core Working Group. I will be forever grateful.
我正在添加的是一种方法来自己发现任何表达式中的三个底层分类类别中的哪一个:左值,x值或prvalue.
#include <type_traits>
#include <typeinfo>
#include <iostream>
#ifndef _MSC_VER
# include <cxxabi.h>
#endif
#include <memory>
#include <string>
#include <cstdlib>
template <typename T>
std::string
expression_name()
{
typedef typename std::remove_reference<T>::type TR;
std::unique_ptr<char, void(*)(void*)> own
(
#ifndef _MSC_VER
__cxxabiv1::__cxa_demangle(typeid(TR).name(), nullptr,
nullptr, nullptr),
#else
nullptr,
#endif
std::free
);
std::string r = own != nullptr ? own.get() : typeid(TR).name();
if (std::is_const<TR>::value)
r += "const ";
if (std::is_volatile<TR>::value)
r += "volatile ";
if (std::is_lvalue_reference<T>::value)
r = "lvalue expression of type " + r;
else if (std::is_rvalue_reference<T>::value)
r = "xvalue expression of type " + r;
else
r = "prvalue expression of type " + r;
return r;
}
以上功能可以像:
std::cout << "some_expression is a " << expression_name<decltype(some_expression)>() << '\n';
它将回答这个OP的问题.例如:
int main()
{
std::cout << "Foo(5) is a " << expression_name<decltype(Foo(5))>() << '\n';
std::cout << "Foo(5).get_addr() is a " << expression_name<decltype(Foo(5).get_addr())>() << '\n';
std::cout << "Foo(78) = Foo(86) is a " << expression_name<decltype(Foo(78) = Foo(86))>() << '\n';
std::cout << "++Foo(5) is a " << expression_name<decltype(++Foo(5))>() << '\n';
std::cout << "++(Foo(78) + Foo(86)) is a " << expression_name<decltype(++(Foo(78) + Foo(86)))>() << '\n';
std::cout << "Foo(78) + Foo(86) is a " << expression_name<decltype(Foo(78) + Foo(86))>() << '\n';
std::cout << "std::move(Foo(5)) is a " << expression_name<decltype(std::move(Foo(5)))>() << '\n';
std::cout << "std::move(++Foo(5)) is a " << expression_name<decltype(std::move(++Foo(5)))>() << '\n';
}
打印出来:
Foo(5) is a prvalue expression of type Foo
Foo(5).get_addr() is a prvalue expression of type void*
Foo(78) = Foo(86) is a lvalue expression of type Foo
++Foo(5) is a lvalue expression of type Foo
++(Foo(78) + Foo(86)) is a lvalue expression of type Foo
Foo(78) + Foo(86) is a prvalue expression of type Foo
std::move(Foo(5)) is a xvalue expression of type Foo
std::move(++Foo(5)) is a xvalue expression of type Foo
使用此功能时要注意的一个方面:
decltype(variable_name)将给出变量名称的声明类型.如果要在使用variable_name时(而不是其声明的类型)发现表达式的值类别,则在decltype中使用时,需要在(variable_name)周围添加额外的括号.那是:
decltype((variable_name))
是表达式variable_name的类型,而不是variable_name的声明类型.
例如给出:
Foo&& foo = Foo(5);
std::cout << "foo is a " << expression_name<decltype(foo)>() << '\n';
这将错误地输出:
foo is a xvalue expression of type Foo
将额外的括号添加到decltype:
std::cout << "foo is a " << expression_name<decltype((foo))>() << '\n';
将foo从类型名称转换为表达式.现在的输出是:
foo is a lvalue expression of type Foo
如果您不确定是否需要添加括号来获得正确的答案,那么只需添加它们即可.添加它们不会使错误的答案正确 – 除非您希望获得变量的声明类型,而不是表达式的类型.在后一种情况下,您需要一个密切相关的功能:type_name<T>()
.