简体   繁体   English

何时使用具有静态存储持续时间的右值引用?

[英]When to use rvalue references with static storage duration?

C++11 allows a rvalue reference to be declared in global(,namespace, class) scope and to have static storage duration. C ++ 11允许在全局(,名称空间,类)范围内声明右值引用,并具有静态存储持续时间。 What would be a good scenario for using such variables? 使用这些变量的好方案是什么? I have made several tests and have come up with a few observations. 我做了几次测试,并提出了一些观察结果。 The following piece of code will not compile (g++ 5.3) 以下代码将无法编译(g ++ 5.3)

double&& m = 3.5;
double&& s() {return m;}

testy.cpp: In function 'double&& s()': testy.cpp:在函数'double && s()'中:

testy.cpp:7:22: error: cannot bind 'double' lvalue to 'double&&' double&& s() {return l;} testy.cpp:7:22:错误:无法将'double'左值绑定到'double &&'double && s(){return l;}

This is rather counterintuitive, even thought we declared a rvalue reference it is treated as if the data is returned by value. 这是违反直觉的,甚至认为我们声明了一个右值引用,它被视为数据按值返回。 This is, of course, a contrived example and indeed returning a rvalue reference is rare. 当然,这是一个人为的例子,实际上返回一个右值参考是很少见的。 However why does it not work? 但是为什么它不起作用? My interpretation is that by declaring the rvalue reference m with static storage duration we get a form of "universal reference", a term coined by Scott Meyers (more info here ). 我的解释是,通过使用静态存储持续时间声明右值参考m ,我们得到了一种“通用参考”形式,这是由Scott Meyers创造的一个术语(更多信息在这里 )。 One of the properties of a universal reference is that it can bind to both lvalues and rvalues. 通用引用的一个属性是它可以绑定到左值和右值。 However unlike "universal references", which rely on reference collapse due to type substitutions, the mechanism which is involved in the resolution above is unclear to me. 然而,与依赖于由于类型替换引起的参考崩溃的“通用参考”不同,我所不清楚上述解决方案中涉及的机制。 In fact the following code compiles without problems. 事实上,以下代码编译没有问题。

double&& m = 3.5;

int main() {
    double a {4.2};
    double& b = a;
    m = a;
    m = b;
    m = 3.2;
}

Hence, as stated above, m binds to every flavor of double . 因此,如上所述, m双重的每种风味结合。 However the fact that the variable has static storage duration seems to contradict the nature of rvalues, which bind to temporary object. 但是,变量具有静态存储持续时间的事实似乎与绑定到临时对象的rvalues的性质相矛盾。 There are few questions that arise. 很少有问题出现。 Why is that allowed? 为什么允许这样做? What scenario will make use of this property? 什么情况会利用这个属性? . My first guess was that we can use that in providing template specializations which accept any "flavour" of argument, much the same way universal references do, with the exception that they are bound more strongly to the underlying type. 我的第一个猜测是,我们可以使用它来提供模板特化,它接受任何参数的“味道”,就像通用引用一样,除了它们与底层类型绑定得更强。 Moreover the scope of the "static rvalue reference" seems not to matter, ie rvalues with static storage duration and "function" scope also exhibit the behavior as demonstrated below. 此外,“静态右值参考”的范围似乎无关紧要,即具有静态存储持续时间和“功能”范围的右值也表现出如下所示的行为。

int main() {
    static double&& a = 4.2;
    double b = 3.2;
    double& c = b;
    a = b;
    a = c;
}

The reason it can't compile is in the error message: 它无法编译的原因是在错误消息中:

testy.cpp:7:22: error: cannot bind 'double' lvalue to 'double&&' double&& s() {return l;} testy.cpp:7:22:错误:无法将'double' 左值绑定到'double &&'double && s(){return l;}

Named variables are always lvalues - l is an lvalue. 命名变量总是左值 - l是左值。 s() returns an rvalue. s()返回一个右值。 You'd have to use a cast to make this work: 你必须使用强制转换来完成这项工作:

double&& s() {return std::move(m); }

Why is that allowed? 为什么允许这样做? What scenario will make use of this property? 什么情况会利用这个属性?

Is it really worth adding an exception to the language explicitly banning it? 是否真的值得在明确禁止它的语言中添加一个例外?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM