【发布时间】:2011-01-16 09:05:54
【问题描述】:
前段时间,当我想编写is_callable<F, Args...> trait 时,我对某些代码的以下行为感到困惑。重载解析不会调用通过非常量 ref 接受参数的函数,对吗?为什么它不拒绝以下因为构造函数想要Test&?我预计它会占用f(int)!
struct Test {
Test() { }
// I want Test not be copyable from rvalues!
Test(Test&) { }
// But it's convertible to int
operator int() { return 0; }
};
void f(int) { }
void f(Test) { }
struct WorksFine { };
struct Slurper { Slurper(WorksFine&) { } };
struct Eater { Eater(WorksFine) { } };
void g(Slurper) { }
void g(Eater) { } // chooses this, as expected
int main() {
// Error, why?
f(Test());
// But this works, why?
g(WorksFine());
}
错误信息是
m.cpp: In function 'int main()':
m.cpp:33:11: error: no matching function for call to 'Test::Test(Test)'
m.cpp:5:3: note: candidates are: Test::Test(Test&)
m.cpp:2:3: note: Test::Test()
m.cpp:33:11: error: initializing argument 1 of 'void f(Test)'
你能解释一下为什么一个有效而另一个无效吗?
【问题讨论】:
-
它在我的 MSVC++ 10 编译器上编译得很好。
-
好吧...我明白了..“Fehler”的意思是“错误”..“Anmerkung”的意思是“注意”..哈哈..学习新的人类语言...这是什么语言方式? :|
-
@Nawaz 哎呀,我忘了翻译!
-
@Johannes : 呵呵..没问题...我只是用 gcc 编译的,我得到了翻译:P.. 还有 google...
-
请注意,Comeau 失败并出现同样的错误。
标签: c++ rvalue overload-resolution const-reference