[英]invalid user-defined conversion to rvalue reference
除了4.9.0-4.9.4和9.1.0之外,大多數版本的gcc都認為這個C ++ 11代碼-pedantic
,除非同時使用-pedantic
和-fpermissive
選項,這是什么原因? clang編譯它。
struct A {
int a;
operator int() && { return a; }
operator int&() & { return a; }
};
void b(int &&) {}
int main()
{
b(A{});
}
輸出類似於:
prog.cc: In function 'int main()':
prog.cc:11:10: error: invalid user-defined conversion from 'A' to 'int&&' [-fpermissive]
b(A{});
^
prog.cc:4:5: note: candidate is: A::operator int&() & <near match>
operator int&() & { return a;
根據StoryTeller的評論,問題顯然與實施中的中間錯誤有關,並且他們修復了,可能的解決方案可能是:
#include <utility> //std::move
#include <iostream>
struct A {
int a;
operator int const& () const { std::cout <<__PRETTY_FUNCTION__<<std::endl; return a; }
operator int&&() && { std::cout <<__PRETTY_FUNCTION__<<std::endl; return std::move(a); }
};
void b(int &&) {}
void b(const int &) {}
int main()
{
b(A{});
b(std::move(A{}));
A a;
b(a);
}
輸出:
A::operator int&&() && A::operator int&&() && A::operator const int&() const
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.