[英]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.