从4.1/2
由左值表示的对象中包含的值是右值结果.当在sizeof(5.3.3)的操作数内发生左值到右值转换时,不会访问引用对象中包含的值,因为该运算符不会计算其操作数.
出于好奇,我想知道,同样适用于if,for,while语句,即当评估语句的结果时,左值是否转换为rvalue?
我很难理解这段代码(C++ 14草案标准[conv.lval]中的一个例子)是如何调用未定义的行为的g(false).为什么constexpr让程序有效?
另外,"不访问y.n" 是什么意思?在两个调用中g()我们都返回n数据成员,为什么最后一行说它不访问它?
struct S { int n; };
auto f() {
S x { 1 };
constexpr S y { 2 };
return [&](bool b) { return (b ? y : x).n; };
}
auto g = f();
int m = g(false); // undefined behavior due to access of x.n outside its
// lifetime
int n = g(true); // OK, does not access y.n
Run Code Online (Sandbox Code Playgroud) 我想当一个通用引用参数与一个右值引用参数匹配时,会返回一个右值引用参数.但是,我的测试显示rvalue引用被通用引用函数模板转换为左值引用.为什么会这样?
#include <iostream>
#include <type_traits>
using namespace std;
template <typename T>
T f1(T&&t) { //<-----this is a universal reference
cout << "is_lvalue reference:" << is_lvalue_reference<T>::value << endl;
cout << "is_rvalue reference:" << is_rvalue_reference<T>::value << endl;
cout << "is_reference:" << is_reference<T>::value << endl;
return t;
}
void f2(int&& t) {
cout << "f2 is_lvalue reference:" << is_lvalue_reference<decltype(t)>::value << endl;
cout << "f2 is_rvalue reference:" << is_rvalue_reference<decltype(t)>::value << endl;
cout << "f2 is_reference:" << is_reference<decltype(t)>::value << endl;
f1(t);
}
int main()
{ …Run Code Online (Sandbox Code Playgroud) c++ rvalue-reference lvalue-to-rvalue perfect-forwarding c++11
我遇到一个问题,gcc编译器将局部变量(非临时)作为函数的rvalue参数移动.我有一个简单的例子:
class A
{
public:
A() {}
A& operator=(const A&) { std::cout << "const A&\n"; return *this; }
A& operator=(A&&) { std::cout << "A&&\n"; return *this; }
};
class B
{
public:
B() {}
B& operator=(const B&) { std::cout << "const B&\n"; return *this; }
B& operator=(B&&) { std::cout << "B&&\n"; return *this; }
template<class T> B& operator=(const T&) { std::cout << "const T& (T is " << typeid(T).name() << ")\n"; return *this; }
template<class T> B& operator=(T&&) { std::cout …Run Code Online (Sandbox Code Playgroud) c++ templates lvalue-to-rvalue move-assignment-operator c++14
我正在开发一个处理大量原子操作的项目。到目前为止,我还不知道\xe2\x80\x99t atomic_load(),并且仅依靠赋值运算符来获取原子类型的值,并且除了这么多测试之外,我还没有\xe2\x80\x99t看到错误。这些原子类型会被多个进程和线程以及 更改atomic_compare_exchange_strong_explicit(),因此它们每次都需要一个旧值,而 \xe2\x80\x99s 我总是这样做,oldValue = <Atomic_ type_variable>并且它总是工作正常。\n这只是偶然吗?我应该更喜欢使用atomic_load()吗?
假设您已将2 vectors传递给函数lvalue references。后来您意识到,可以使用递归并vectors使用它们的切片iterators。
难道是一个恰当的策略,如果我继续前进,写一些util功能,利用这些vecotrs作为rvalues?还是我应该以任何方式避免这种情况?
简化模式:
Node* util(vector<int>&& a, vector<int>&& b) {
Node* root = new Node(a[0]);
root->left = util({ a.begin(), a.end() }, { b.begin(), b.end() });
root->right = util({ a.begin(), a.end() }, { b.begin(), b.end() });
return root;
}
Node* main(vector<int>& a, vector<int>& b) {
return util({ a.begin(), a.end() }, { b.begin(), b.end() });
}
Run Code Online (Sandbox Code Playgroud)
实际示例(LeetCode 105):
TreeNode* util(vector<int>&& preorder, vector<int>&& inorder) {
if …Run Code Online (Sandbox Code Playgroud) 我对以下代码感到困惑:
#include <iostream>
int main()
{
int x{};
int&& rvx = static_cast<int&&>(x);
++rvx;
std::cout << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它的输出是1.我不明白这是如何工作的.所述static_cast应该左值铸造x成一个x值,然后将其分配给rvx.为什么递增rvx会导致变化x?这是因为转换后的左值到右值基本上位于相同的内存位置,但它现在只被视为右值?我的印象(这可能是假的),不知何故,演员创造了一个临时的参数.