【发布时间】:2012-11-21 20:29:31
【问题描述】:
我尝试查阅do_run的解析标准,发现“对于使用非限定名查找(3.4.1)或限定名查找(3.4.3)的部分查找,只有 从模板定义上下文中找到函数声明”。上下文到底是什么?
在下面的例子中,do_run(int) 以某种方式“隐藏”了do_run(domain::mystruct),编译器抱怨o can't be converted to int。如果我注释掉do_run(int),do_run(domain::mystruct) 对run 可见,并且代码被编译。这种行为是否与标准中提到的“上下文”有关?在我看来,do_run(int) 和 do_run(domain::mystruct) 都应该对(可解析的)运行可见。
namespace domain {
struct mystruct { };
}
void do_run(domain::mystruct) { cout << "do_run(domain::mystruct)" << endl; }
namespace lib { namespace details {
template <class T>
class runner {
public:
void run(T t) { do_run(t); }
};
void do_run(int) { cout << "do_run(int)" << endl; }
}}
int main() {
domain::mystruct o;
lib::details::runner<domain::mystruct> r;
r.run(o);
return 0;
}
在存在do_run(int) 的情况下,我需要一个额外的步骤来将do_run(domain::mystruct) 带入“上下文”。有三种方式:
- 将
do_run(domain::mystruct)放入命名空间域中。 - 将
do_run(domain::mystruct)放入命名空间lib::details。 - 在命名空间 lib::details 中添加
using ::do_run。
所以我推断上下文是命名空间 lib::details 和命名空间域?
编译器VS2010
【问题讨论】:
标签: c++ templates namespaces argument-dependent-lookup