我不得不花一些时间来查找和修复我设法在以下代码中隔离的错误:
#include <iostream>
struct A
{
std::string S;
A(const std::string s) { S = s; }
};
void f1(A a) { std::cout << "f1:a.S = " << a.S << "\n"; }
void f1(const std::string s) { std::cout << "f1:s = " << s << "\n"; }
void f2(A a) { std::cout << "f2:a.S = " << a.S << "\n"; }
int main()
{
f1(A("test"));
f1(std::string("test"));
f2(A("test"));
f2(std::string("test"));
return 0;
}
该错误是由f1
-function 创建的被忽略的(由我和编译器(?))歧义引起的:f2
清楚地表明 bothf1(A)
和f1(std::string)
apply to A
,但是编译时编译器不会拾取歧义,并且在执行时输出是:
f1:a.S = test
f1:s = test
f2:a.S = test
f2:a.S = test
这种行为正确吗?编译器问题?还是只是普通的旧 PIBCAK?