Reputation: 11181
I am writing a simple wrapper class, and I want to provide explicit conversion operators to the wrapped type. The following code compiles fine with gcc
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 clang
reports an 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 latest draft, 5.2.9 §4) static_cast<T>(e)
has the same semantic has T t(e)
, but clang does not refuse the latter.
Which compiler is right?
Upvotes: 4
Views: 749
Reputation: 302932
This is clang bug 19917. From the section of the standard you mentioned, §5.2.9/4:
An expression
e
can be explicitly converted to a typeT
using astatic_cast
of the formstatic_cast<T>(e)
if the declarationT t(e);
is well-formed, for some invented temporary variablet
.
In this case, T t(e);
is well-formed and compiles on both compilers, so static_cast<T>(e)
should too. GCC correctly accepts it.
Upvotes: 3