While experimenting a bit with C++ templates I managed to produce this simple code, for which the output is different, than I expected according to my understanding of C++ rules.
void bar(double d)
{
std::cout << "bar(double) function called" << std::endl;
}
template <typename T> void foo(T t)
{
bar(3);
}
void bar(int i)
{
std::cout << "bar(int) function called" << std::endl;
}
int main()
{
foo(3);
return 0;
}
When I compile this code in VC++2008 Express, function bar(int)
gets called. That would be the behaviour, I would expect if bar(3);
in the template body was dependent on the template parameter. But it's not. The rule I found here says "The C++ standard prescribes that all names that are not dependent on template parameters are bound to their present definitions when parsing a template function or class". Am I wrong, that "present definition" of bar
when parsing the template function foo
is the definition of
void bar(double d);
? Why it's not the case if I am wrong. There are no forward declarations of bar
in this compilation unit.