Recently I've bumped into a weird behavior when having a class with a template constructor that accepts only one argument. This class under certain circumstances fails to initialize at all.
#include <iostream>
struct Foo
{
template<typename T>
Foo(const T& x)
{
std::cout << "--" << x << std::endl;
}
};
int main(int, char**)
{
Foo f(std::string());
std::cout << "++" << f << std::endl;
return 0;
}
What I have is a Foo structure with the constructor you see. In main I create a new Foo instance with an empty std::string. The example compiles and the output is:
++1
WTF??? As you may have noticed there are three problems with the this. First of all the constructor was never called, secondly the f was printed as "1", thirdly there is no overloaded operator<< that prints the Foo using the cout and despite that the compiler never complained. This means that the f is not a type of Foo.
I change the example slightly:
int main(int, char**)
{
Foo f(std::string(""));
std::cout << "++" << f << std::endl;
return 0;
}
...and the compiler throws an error that there is no overloaded operator<< and that is normal.
int main(int, char**)
{
Foo f(std::string("Hello"));
return 0;
}
Now the output is:
--Hello
and that's normal again.
The problem is when having passing to Foo::Foo a an empty object whether it is std::string() or float() or any_type(). I've tested the example in GCC 4.4 and 4.6 and I find this behavior very unexpected.
What is your opinion? Is this a GCC bug or am I missing something here?