为什么这个左值调用不明确?

为什么这个左值调用不明确?我可以获得AA和AA&并且编译器将知道使用
AA&
。但是,当我添加第三个选项时,我得到一个错误。显然AA&&是一个更好的重载然后其他像int这样的int更好然后很长。为什么这个含糊不清?有没有办法我可以保持所有3个重载并明确我想要哪一个? (Typecasting
(AA&&)
不会这样做)。
struct AA{
    void*this_;
    AA() { this_=this; }
    //not valid, use AA&, AA(AA a){ this_=this; }
    AA(AA&a){ this_=this; }
    AA(AA&&a){ this_=a.this_; }
};
void movetest(AA s) {}
void movetest(AA& s) {}
//This gets me the ambiguous error void movetest(AA&& s) {}
AA&& movetest() { return AA(); }
void MyTestCode2(){
    AA a;
    AA b(a);
    AA c = movetest();
    movetest(AA());
}
    
已邀请:
  我可以获得AA和AA&和编译器   会知道使用AA& 是的,在movetest(AA());的情况下,只有movetest(AA)是可行的,因为对非const的(左值)引用不能绑定到右值。但是,rvalue引用被称为直接绑定到临时引用。因此,用于过载解决目的的功能
void movetest(AA)
void movetest(AA&&)
因为用于将AA()分别转换为AA和AA&&的隐式转换序列是相等的。前者并不是更好,因为直接引用绑定也被认为是身份转换。     
同意decltype。这与C ++ 03/98模糊性没什么不同:
struct AA {};

void movetest(AA s) {}
void movetest(AA& s) {}

int main()
{
    AA a;
    movetest(a);
}

test.cpp:9:5: error: call to 'movetest' is ambiguous
    movetest(a);
    ^~~~~~~~
test.cpp:3:6: note: candidate function
void movetest(AA s) {}
     ^
test.cpp:4:6: note: candidate function
void movetest(AA& s) {}
     ^
1 error generated.
    

要回复问题请先登录注册