当我在clang下遇到问题时,我一直在开发一种适配器类.如果定义了左值引用和右值引用的转换运算符,则会在尝试从类中移动时出现歧义编译错误(当此类代码应该没问题时,
operator const T& () const&
Run Code Online (Sandbox Code Playgroud)
只适用于左手AFAIK).我用简单的例子重现了错误:
#include <string>
class StringDecorator
{
public:
StringDecorator()
: m_string( "String data here" )
{}
operator const std::string& () const& // lvalue only
{
return m_string;
}
operator std::string&& () && // rvalue only
{
return std::move( m_string );
}
private:
std::string m_string;
};
void func( const std::string& ) {}
void func( std::string&& ) {}
int main(int argc, char** argv)
{
StringDecorator my_string;
func( my_string ); // fine, operator std::string&& not allowed
func( std::move( …Run Code Online (Sandbox Code Playgroud)