What does is_constructible_v<std::string&&, std::string&&> mean by?

295 views Asked by At

I can understand what is is_constructible_v<std::string, std::string&&>. But what does is_constructible_v<std::string&&, std::string&&> mean by?

What is difference between is_constructible_v<std::string, std::string&&> and is_constructible_v<std::string&&, std::string&&>?

I think that is_constructible_v<std::string&&, std::string&&> means that construct rvalue from rvalue. But I'm not clear what does constructing rvalue mean. If T is just constructible, it's constructor always could be used as rvalue. Is this the meaning of constructing rvalue?

2

There are 2 answers

8
Ted Lyngmo On

std::is_constructible_v<std::string&&, std::string&&> tests if

std::string&& obj(std::declval<std::string&&>());

is well-formed (see [meta.unary.prop] p9), which it is. You are allowed to create an rvalue reference like so:

std::string&& obj(std::string{}); // string&& from string&&
5
HolyBlackCat On

&/&& in the first and the remaining parameters of is_constructible have different meanings.

In the second (and any following) parameters, they specify the value category: & for lvalue and /&& for rvalue.

In the first parameter, &/&& are a part of the type of the variable we're constructing, not a value category.

So, in is_constructible_v<std::string, std::string&&> we're constructing a std::string from an rvalue of type std::string.

And in is_constructible_v<std::string&&, std::string&&>, we're constructing a std::string&& (a reference, not a string) from an rvalue of type std::string.


Here's an example how it could happen:

std::string a;
std::string &&b = std::move(a); // `move` here returns an rvalue of type `std::string &&`

Confusingly, && near b and && in the return type of move also mean subtly different things. The former makes b a reference. While the latter makes move return an xvalue, as opposed to prvalue (when returning by value) or lvalue (when returning a & reference). The latter doesn't produce any references, strictly speaking.