Cpp*_*oob 9 c++ rvalue rvalue-reference xvalue c++11
这是一些示例代码:
#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
}
Run Code Online (Sandbox Code Playgroud)
表达式如Foo(5)prvalues或一般rvalues?我可以在这些表达式上调用get_addr()的事实是否意味着它们具有身份?或者我不能应用默认&-operator(我的意思是非重载)意味着他们没有身份,因此是prvalues?
是否公平地说通过产生它的表达式产生的值的可变性与这个值分类是正交的?
How*_*ant 17
每个表达式都是一个,只有一个:
作为左值和x值的表达式的并集统称为glvalues.
作为xvalues和prvalues的表达式的并集统称为rvalues.
因此,xvalue表达式都被称为glvalues和rvalues.
在Alf的回答中找到的方便图表正确地说明了我用上面的单词描述的关系,也可以在C++标准的第3.10节,C++ 11及更高版本中找到.
我上面所说的一切,我怀疑OP已经知道,只是从这个问题的标题的措辞.
琐事:
Bjarne Stroustrup发明了这种表达式分类,并且这样做可能使整个右值参考提议免于核心工作组的崩溃.我将永远感激.
我正在添加的是一种方法来自己发现任何表达式中的三个底层分类类别中的哪一个:左值,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;
}
Run Code Online (Sandbox Code Playgroud)
以上功能可以像:
std::cout << "some_expression is a " << expression_name<decltype(some_expression)>() << '\n';
Run Code Online (Sandbox Code Playgroud)
它将回答这个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';
}
Run Code Online (Sandbox Code Playgroud)
打印出来:
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
Run Code Online (Sandbox Code Playgroud)
使用此功能时要注意的一个方面:
decltype(variable_name)将给出变量名称的声明类型.如果你想发现的价值类表达式时variable_name使用(而不是其声明的类型),那么你需要添加额外的左右括号(variable_name)中使用时decltype.那是:
decltype((variable_name))
Run Code Online (Sandbox Code Playgroud)
是表达式 的类型variable_name,而不是声明的类型variable_name.
例如给出:
Foo&& foo = Foo(5);
std::cout << "foo is a " << expression_name<decltype(foo)>() << '\n';
Run Code Online (Sandbox Code Playgroud)
这将错误地输出:
foo is a xvalue expression of type Foo
Run Code Online (Sandbox Code Playgroud)
将额外的括号添加到decltype:
std::cout << "foo is a " << expression_name<decltype((foo))>() << '\n';
Run Code Online (Sandbox Code Playgroud)
转换foo从类型名称到表达.现在的输出是:
foo is a lvalue expression of type Foo
Run Code Online (Sandbox Code Playgroud)
如果您不确定是否需要添加括号来获得正确的答案,那么只需添加它们即可.添加它们不会使错误的答案正确 - 除非您希望获得变量的声明类型,而不是表达式的类型.在后一种情况下,你需要一个密切相关的功能:type_name<T>().
在C++中的任何表达式是任一的左值或右值.因此,您要求的是rvalues的分类.为此,请在C++ 11标准§3.10/ 1中检查显示分类树的图.

欲了解更多信息(不钻研标准)看到什么是右值,左值,....
关于
"表达式如Foo(5)rvalues或prvalue"
一个prvalue是必要的rvalue - 因为它不可能是一个左值.
甲prvalue "('纯’右值)是一个rvalue这不是一个x值",和x值是一个构造呼叫不产生一个rvalue参考"某些种涉及rvalue引用的表达式的结果",因此,它不是一个x值.所以rvalue是一个prvalue,一个纯rvalue.
| 归档时间: |
|
| 查看次数: |
1313 次 |
| 最近记录: |