Static_cast with explicit rvalue conversion operator

I am writing a simple wrapper class and I want to provide explicit conversion operators to a wrapped type. The following code compiles withgcc

class wrap
{
    double value;
public:
    explicit wrap(double x) : value(x) {}
    explicit operator double&&() && { return std::move(value); }
};

int main() {
    wrap w(5);
    double && x (std::move(w) ); //ok
    double && y = static_cast<double&&>(std::move(w)); //clang reports an error here
}

But clangreports error: cannot cast from lvalue of type 'typename std::remove_reference<wrap &>::type' (aka 'wrap') to rvalue reference type 'double &&'; types are not compatible.

As far as I know (see the last draft, 5.2.9 §4 ) it static_cast<T>(e)has the same semantics T t(e), but clang does not abandon the latter.

Which compiler is right?

+4
source share
1 answer

This is clang 19917 error . From the section of the standard you mentioned, §5.2.9 / 4:

e T, static_cast static_cast<T>(e) T t(e); , T.

T t(e); , static_cast<T>(e) . GCC .

+3

Source: https://habr.com/ru/post/1569342/


All Articles