I had to spent some time in finding and fixing a bug that I managed to isolate in the following code:
#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;
}
The bug was caused by the overlooked (by me and the compiler(?)) ambiguity created by the f1
-function: f2
clearly shows that both f1(A)
and f1(std::string)
apply to A
, but when compiled the ambiguity is not picked-up by the compiler, and when executed the output is:
f1:a.S = test
f1:s = test
f2:a.S = test
f2:a.S = test
Is this behavior correct? Compiler problem? Or just plain old PIBCAK?
f1(std::string)
expectingA
and not getting it :( – Plantation