C++11 - 区分右值指针

发布于 2024-10-22 07:37:52 字数 489 浏览 7 评论 0原文

如何将变量区分为编译器构造的字符串?

例如,右值 "Hello, World" 的类型为 const char*const char* 本身并不意味着指针不能更改。 char* const 指针无法更改,但这不是编译器构造的。

这是否意味着,对于任何保存 const char* 的容器,应该通过 C++ 移动语义以外的方式复制数据?有什么方法可以只移动编译器构造的字符串并保留所有其他字符串吗?

例如,在 GCC 4.5.2 中,返回类型 int(而不是 int&)的方法被视为返回 int&& >。我不知道实际的标准是否应该是这样,但这就是 GCC 目前所做的。

编辑:为了澄清,我的意思是应该复制指针指向的实际内存。这意味着必须分配新的内存,并且应该将指针中的数据复制到新位置。

How can I distinguish a variable as a compiler-constructed string?

For example, while the rvalue "Hello, World" is of type const char*. const char* in itself does not mean that a pointer can't be changed. A char* const pointer can't be changed, but that's not what's constructed by the compiler.

Does this mean that, for any container that holds a const char*, the data should be copied by means other than C++'s move semantics? Is there any way to just move compiler-constructed strings and leave all other strings alone?

For example, in the GCC 4.5.2, a method that returns the type int as opposed to int& is treated as returning int&&. I don't know if the actual standard is supposed to be this way, but that's what the GCC does for the time being.

Edit: To clarify, I mean that the actual memory that the pointer points to should be copied. This means that new memory has to be allocated and that the data from the pointer should be copied to the new location.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初吻给了烟 2024-10-29 07:37:53

“Hello, World” 不是 const char* 类型。它的类型为 const char[13],并且是左值,而不是右值。

当您在隐式转换为指向其初始元素的 const char* 的上下文中使用 "Hello, World" 时,生成的指针是一个右值(因为它是隐式转换产生的临时对象。

例如,在 GCC 4.5.2 中,返回类型 int(而不是 int&)的方法被视为返回 int&&。.

如果您调用按值返回的函数(例如,int),则该函数调用表达式是右值表达式。如果您调用返回左值引用的函数(例如,int&),则该函数调用表达式是左值表达式。

如何区分变量是编译器构造的字符串?

你不能,真的:"Hello, World" 和你可能声明的任何其他 const char[13] 之间没有区别。

至于在标准库容器(如 std::vector )中存储 const char* 或任何其他指针类型,该容器不会触及指向的数据:容器将创建、移动、复制和销毁指针。

如果您需要管理指向的数据,您需要自己编写一个类来管理指向的对象(很像智能指针类)。像这样编写一个类来管理资源的习惯称为“资源获取即初始化”(RAII) 或“范围限制资源管理”(SBRM)。

"Hello, World" is not of type const char*. It is of type const char[13] and it is an lvalue, not an rvalue.

When you use "Hello, World" in a context in which it is implicitly converted to a const char* pointing to its initial element, the resulting pointer is an rvalue (because it is a temporary object resulting from an implicit conversion.

For example, in the GCC 4.5.2, a method that returns the type int as opposed to int& is treated as returning int&&.

If you call a function that returns by value (e.g., int), then that function call expression is an rvalue expression. If you call a function that returns an lvalue reference (e.g., int&), then that function call expression is an lvalue expression.

How can I distinguish a variable as a compiler-constructed string?

You can't, really: there's no difference between "Hello, World" and any other const char[13] that you might declare.

As for storing const char*s or any other pointer types in a Standard Library container like std::vector, the container isn't going to touch the pointed-to data: the container is just going to create, move, copy, and destroy the pointers.

If you need to manage the pointed-to data, you need to do that yourself by writing a class to manage the pointed-to object (much like a smart pointer class). The idiom of writing a class to manage a resource like this is called "Resource Acquisition is Initialization" (RAII) or "Scope-Bound Resource Management" (SBRM).

傻比既视感 2024-10-29 07:37:53

这是否意味着,对于任何保存 const char* 的容器,应该通过 C++ 移动语义以外的方式复制数据?

这意味着指针被复制。不多不少。必须复制指针才能让容器工作。

Does this mean that, for any container that holds a const char*, the data should be copied by means other than C++'s move semantics?

It means that the pointer is copied. Nothing more, nothing less. The pointer must be copied for the container to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文