【发布时间】:2011-08-01 05:48:58
【问题描述】:
为什么这个右值调用不明确?我可以有 AA 和 AA&,编译器会知道使用 AA&。但是当我添加第三个选项时,我得到了一个错误。显然 AA&& 是一个更好的重载,然后其他像 int 的 int 比 long 更好。为什么这是模棱两可的?有没有办法可以保留所有 3 个重载并明确我想要哪个? (类型转换(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());
}
【问题讨论】:
-
(顺便说一句,您的零参数
movetest函数返回对局部变量的引用。) -
(我也会说通常你不会想要所有三个重载。)
-
@GMan:什么……为什么?嗯?也许你也可以解释一下stackoverflow.com/questions/5591995/…
标签: c++ c++11 rvalue ambiguous move-semantics