函数的参数类型有一个复制构造函数与非const引用选择? [英] Function with parameter type that has a copy-constructor with non-const ref chosen?
问题描述
前一段时间,当我想写一个 is_callable
Test&
?我期望它采取 f(int)
!
Test(){}
//我想测试不能从右值复制!
Test(Test&){}
//但它可以转换为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){} //按预期选择这个
int main(){
//错误,为什么?
f(Test());
//但是这个工作,为什么?
g(WorksFine());
}
错误讯息是
m.cpp:在函数'int main()':
m.cpp:33:11:错误:没有匹配函数调用'Test :: Test测试)
m.cpp:5:3:注意:候选者是:Test :: Test(Test&)
m.cpp:2:3:note:Test :: Test $ b m.cpp:33:11:error:初始化'void f(Test)'的参数1
<
重载解决方案会选择最接近的函数匹配提供的参数。你提供了一个测试。无需转换 - 使用身份转换。因此,函数分辨率选择f(Test)。测试无法从您提供的右值复制,但重载解析已成功...转换为int从不检查。
g(Eater)
,因为类型不完全匹配,不使用标识转换,编译器必须找到一个工作的转换例程。 g(Slurper)
不是因为您无法使用提供的参数。
不会失败: struct A {operator int();}; void f(A&); void f(int); void g(){f(A());}
因为f(A&)不是提供的参数的可行重载。在这种情况下,参数是一个引用,并且temps不绑定到非const的事实被允许影响分辨率。在这种情况下,它会和函数的版本成为一个非候选人,只留下一个,它的工作原理。
Some time ago I was confused by the following behavior of some code when I wanted to write a is_callable<F, Args...>
trait. Overload resolution won't call functions accepting arguments by non-const ref, right? Why doesn't it reject in the following because the constructor wants a Test&
? I expected it to take 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());
}
Error message is
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)'
Can you please explain why one works but the other doesn't?
Overload resolution picks the function that is the closest match to the supplied argument. You supplied a Test. No conversion necessary -- identity conversion used. Thus function resolution chooses f(Test). Test can't be copied from rvalue, which you supplied, but overload resolution has succeeded already...conversion to int is never checked.
g(Eater)
is chosen because the types don't exactly match, the identity conversion is NOT used, and the compiler has to find a conversion routine that works. g(Slurper)
doesn't because you can't make one out of the supplied argument.
"Why doesn't this one fail: struct A { operator int(); }; void f(A&); void f(int); void g() { f(A()); }
"
Because f(A&) is not a viable overload for the supplied argument. In this case the parameter is a reference and the fact that temps don't bind to non-const is allowed to effect the resolution. In this case it does and the that version of the function becomes a non-candidate, leaving only the one and it works.
这篇关于函数的参数类型有一个复制构造函数与非const引用选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!