[英]static_cast with an explicit rvalue conversion operator
我正在編寫一個簡單的包裝類,我想為包裝類型提供顯式轉換運算符。 以下代碼使用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
}
但clang
報告error: cannot cast from lvalue of type 'typename std::remove_reference<wrap &>::type' (aka 'wrap') to rvalue reference type 'double &&'; types are not compatible
error: cannot cast from lvalue of type 'typename std::remove_reference<wrap &>::type' (aka 'wrap') to rvalue reference type 'double &&'; types are not compatible
。
據我所知(參見最新草案, 5.2.9§4 ) static_cast<T>(e)
具有相同的語義T t(e)
,但clang不拒絕后者。
哪個編譯器是對的?
這是clang bug 19917 。 從您提到的標准部分,§5.2.9/ 4:
表達式
e
可以顯式轉換到類型T
使用static_cast
形式static_cast<T>(e)
,如果聲明T t(e);
對於一些發明的臨時變量t
。
在這種情況下, T t(e);
格式良好並在兩個編譯器上編譯,因此static_cast<T>(e)
應如此。 GCC正確接受它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.