C ++ 11 - Distinctive rvalue pointers

How to distinguish a variable as a string built by the compiler?

For example, while the rvalue "Hello, World" is of type const char* . const char* alone does not mean that the pointer cannot be changed. The char* const pointer cannot be changed, but this is not what the compiler created.

Does this mean that for any container containing const char* , the data should be copied using other means than the C ++ move semantics? Is there a way to just move the lines built with the compiler and leave all the other lines alone?

For example, in GCC 4.5.2, a method that returns an int rather than an int& is considered to return an int&& . I don't know if the actual standard should be that way, but what GCC is doing at the moment.

Edit: To clarify, I mean that the actual memory pointed to by the pointer must be copied. This means that it is necessary to allocate new memory and that the data from the pointer be copied to a new location.

+2
c ++ string c ++ 11 rvalue-reference move-semantics
Mar 18 2018-11-11T00:
source share
2 answers

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

When you use "Hello, World" in a context in which it is implicitly converted to const char* pointing to its source element, the resulting pointer is an rvalue (since this is a temporary object that is the result of an implicit conversion.

For example, in GCC 4.5.2, a method that returns an int as opposed to int& is considered to return an int&& .

If you call a function that returns a value (for example, int ), then this function expression expresses the rvalue expression. If you call a function that returns an lvalue reference (for example, int& ), then this function expression expresses the lvalue expression.

How to distinguish a variable as a string built by the compiler?

You cannot, really: there is no difference between "Hello, World" and any other const char[13] that you could declare.

As for storing const char* or any other types of pointers in a standard library container, for example std::vector , the container will not touch the specified data: the container is only going to create, move, copy, and destroy pointers.

If you need to manage the specified data, you need to do it yourself by writing a class to manage the object with a pointer (like a class of smart pointers). The idiom of writing a class to manage a resource like this is called Resource Initialization (RAII) or Limited to Resource Management Area (SBRM).

+8
Mar 18 '11 at 0:40
source share
β€” -

Does this mean that for any container containing const char *, the data should be copied using other means other than the C ++ move semantics?

This means that the pointer is copied. No more, no less. The pointer must be copied for the container to work.

0
Mar 18 '11 at 0:20
source share



All Articles