Starting with the following (using gcc version 4.0.1
):
namespace name {
template <typename T>
void foo(const T& t) {
bar(t);
}
template <typename T>
void bar(const T& t) {
baz(t);
}
void baz(int) {
std::cout << "baz(int)\n";
}
}
If I add (in the global namespace)
struct test {};
void bar(const test&) {
std::cout << "bar(const test&)\n";
}
then, as I expected,
name::foo(test()); // produces "bar(const test&)"
But if I just add
void bar(const double&) {
std::cout << "bar(const double&)\n";
}
it can't seem to find this overload:
name::foo(5.0) // produces "baz(int)"
What's more,
typedef std::vector<int> Vec;
void bar(const Vec&) {
std::cout << "bar(const Vec&)\n";
}
doesn't appear either, so
name::foo(Vec());
gives a compiler error
error: cannot convert ‘const std::vector<int, std::allocator<int> >’ to ‘int’ for argument ‘1’ to ‘void name::baz(int)’
Is this how the lookup is supposed to work? (Note: if I remove the namespace name
, then everything works as I expected.)
How can I modify this example so that any overload for bar
is considered? (I thought that overloads were supposed to be considered before templates?)