简体   繁体   English

为什么静态const char * const变量是左值时可绑定到右值引用参数?

[英]Why is a static const char * const variable bindable to an rvalue reference parameter when it is an lvalue?

Given static const char * const x = "test"; 给定static const char * const x = "test"; and a function with the signature void DoSomething(std::string && value) , why is it legal to bind this lvalue to the parameter like so DoSomething(x); 以及带有签名void DoSomething(std::string && value)的函数,为什么将此左值绑定到参数是合法的,就像DoSomething(x); ?

I was under the impression that the string literal is an array of char but it decays to the pointer type and is still an lvalue. 我的印象是字符串文字是一个char数组,但是它会衰减为指针类型,并且仍然是左值。 I'm just confused why this is legal. 我只是困惑为什么这是合法的。

When the function with an rvalue reference parameter expects to take ownership of the parameter's data, how does this work with memory in read only segments of say a PE file? 当带有右值引用参数的函数希望获得该参数数据的所有权时,这如何与PE文件的只读段中的内存一起使用? I understand the memory isn't physically moved, but it seems like this would cause problems. 我了解内存并未物理移动,但似乎会引起问题。

std::string is different to const char * . std::stringconst char *不同。 When you initialize a reference with an expression of different type that it can't bind directly to, then a temporary is created which has the correct type for the reference. 当使用无法直接绑定的其他类型的表达式初始化引用时,将创建一个临时类型,该临时类型具有正确的引用类型。 The temporary is initialized by the initializer provided, and the reference is bound directly to the temporary. 临时文件由提供的初始化程序初始化,并且引用直接绑定到临时文件。 Rvalue references can bind to temporaries. 右值引用可以绑定到临时对象。

The function could then take ownership of the temporary's data. 然后,该功能可以获取临时数据的所有权。 The string literal is unchanged (because the constructor string::string(const char *) does not change the literal, and instead takes a copy of its content). 字符串文字不变(因为构造函数string::string(const char *)不会更改文字,而是获取其内容的副本)。

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

相关问题 rvalue或lvalue(const)引用参数 - rvalue or lvalue (const) reference parameter 将右值参数传递给非常量左值引用的参数 - Passing rvalue argument to parameter of non-const lvalue reference 什么时候比rvalue参考模板更喜欢const lvalue引用 - When to prefer const lvalue reference over rvalue reference templates 返回常量左值引用右值临时? 为什么这样做? - Returning const lvalue reference to rvalue temporary? Why does this work? 为什么const / nonconst左值引用都绑定到右值引用? - Why both const/nonconst lvalue references bind to a rvalue reference? 传递const左值参考作为右值参考 - Passing a const lvalue reference as rvalue reference 将rvalue引用传递给const lvalue reference paremeter - Passing rvalue reference to const lvalue reference paremeter 为什么此代码会给出错误“无法将 'char*&' 类型的非 const 左值引用绑定到 'char*' 类型的右值” - Why does this code give the error “cannot bind non-const lvalue reference of type ‘char*&’ to an rvalue of type ‘char*’” 将非常量左值引用绑定到右值 - Bind non-const lvalue reference to rvalue 为什么在重载解析期间const左值引用优先于const右值引用 - Why const lvalue reference has priority over const rvalue reference during overloading resolution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM