从以下内容开始(使用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";
}
}
如果我添加(在global命名空间)
struct test {};
void bar(const test&) {
std::cout << "bar(const test&)\n";
}
然后,正如我所料,
name::foo(test()); // produces "bar(const test&)"
但如果我只是添加
void bar(const double&) {
std::cout << "bar(const double&)\n";
}
它似乎找不到这个重载:
name::foo(5.0) // produces "baz(int)"
更重要的是,
typedef std::vector<int> Vec;
void bar(const Vec&) {
std::cout << "bar(const Vec&)\n";
}
也没有出现,所以
name::foo(Vec());
给出编译器错误
error: cannot convert ‘const std::vector<int, std::allocator<int> >’ to ‘int’ for argument ‘1’ to ‘void name::baz(int)’
这是查找应该如何工作的吗? (注意:如果我删除命名空间name
,然后一切都按我的预期进行。)
我如何修改这个示例,以便任何重载bar
被认为? (我认为应该考虑重载before模板?)