【发布时间】:2012-11-13 07:48:18
【问题描述】:
我不得不花费一些时间来查找和修复我在以下代码中设法隔离的错误:
#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创建的被忽略的(由我和编译器(?))引起的,清楚地表明f1(A)和f1(std::string)都适用于A,但是编译时编译器不会识别歧义,执行时输出是:
f1:a.S = test
f1:s = test
f2:a.S = test
f2:a.S = test
这种行为正确吗?编译器问题?还是只是普通的旧 PIBCAK?
【问题讨论】:
-
@Mat:是的,当我打电话给
f1(std::string)期待A却没有得到它时,就会出现问题:(