可能的重复:
为什么 ADL 找不到函数模板? https://stackoverflow.com/questions/2953684/why-doesnt-adl-find-function-templates
Calling get
似乎没有调用参数相关的查找:
auto t = std::make_tuple(false, false, true);
bool a = get<0>(t); // error
bool b = std::get<0>(t); // okay
g++ 4.6.0 说:
error: 'get' was not declared in this scope
Visual Studio 2010 说:
error C2065: 'get': undeclared identifier
Why?
这是因为你试图明确地实例化get
函数模板,通过提供0
作为模板参数。对于模板,如果具有该名称的函数模板在调用时可见,ADL 就会起作用。这个可见的函数模板仅有助于触发ADL(不得使用actually)然后,可以在其他命名空间中找到最佳匹配。
请注意,函数模板triggers(或启用)ADL,不需要有定义:
namespace M
{
struct S{};
template<int N, typename T>
void get(T) {}
}
namespace N
{
template<typename T>
void get(T); //no need to provide definition
// as far as enabling ADL is concerned!
}
void f(M::S s)
{
get<0>(s); //doesn't work - name `get` is not visible here
}
void g(M::S s)
{
using N::get; //enable ADL
get<0>(s); //calls M::get
}
In g()
, 名字N::get
呼叫时触发 ADLget<0>(s)
.
Demo : http://ideone.com/83WOW http://ideone.com/83WOW
C++ (2003) 第 §14.8.1/6 节读取,
[注意:对于简单函数名称,即使函数名称在调用范围内不可见,参数相关查找 (3.4.2) 也适用。这是因为该调用仍然具有函数调用的语法形式(3.4.1)。但是,当使用具有显式模板参数的函数模板时,调用不具有正确的语法形式,除非在调用时存在可见的具有该名称的函数模板。如果没有这样的名称可见,则该调用在语法上不正确,并且依赖于参数的查找不适用。如果某些此类名称可见,则应用参数相关查找,并且可以在其他命名空间中找到其他函数模板。
[例子:
namespace A {
struct B { };
template<int X> void f(B);
}
namespace C {
template<class T> void f(T t);
}
void g(A::B b) {
f<3>(b); //ill-formed: not a function call
A::f<3>(b); //well-formed
C::f<3>(b); //ill-formed; argument dependent lookup
// applies only to unqualified names
using C::f;
f<3>(b); //well-formed because C::f is visible; then
// A::f is found by argument dependent lookup
}
—结束示例] —结束注释]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)