Reputation: 29
"Hello."
is a temporary that's constructed, copied into std::string
and subsequently destroyed. We can skip copying and go straight to moving. But any decent compiler will elide the copy. So is there any point to the following:
std::string s(std::move("Hello."));
Upvotes: 0
Views: 86
Reputation: 44238
Your code does not make much sense as it would be logically equal to this:
Foo bar( std::move( 123 ) );
There is no point to move constant, and string literal is a constant. Better question would be, and I think is that what you really meant by your question:
class Foo { ... };
class Bar {
public:
Bar( const Foo &foo );
Bar( Foo &&foo );
...
};
Bar bar( std::move( Foo() ); // Does it make sense?
So actually for this case std::move
can be used as a syntax sugar to avoid function declaration of the form:
Bar bar( Foo() );
But it would not make any optimization and in this case:
Bar bar = std::move( Foo() );
It would be completely redundant
Upvotes: 0
Reputation: 31435
If that's even legal and I don't know if it is, it is not optimising anything.
The literal is created at compile time in static space. It can't be moved. string
will always own a copy of the data and not just "refer" to it.
Upvotes: 0
Reputation: 254431
No, there's no point moving a string literal. It's a static array (not a "temporary that's constructed"), which can't be moved, only copied.
Upvotes: 3