考虑模板参数的参数相关外观 (ADL)?
Argument Dependent Look (ADL) considering template arguments?
我有几个命名空间,每个命名空间都有一个名为f
的函数模板。
// f() and Widget
namespace A {
struct Widget { };
template <typename T>
void func(const T&) { }
}
// f() and caller()
namespace B {
template <typename T>
void func(const T&) { }
template <typename T>
void caller(const T& t) {
func(t); // error occurs here
}
}
template <typename T>
class Wrap { };
int main() {
Wrap<A::Widget> w{};
B::caller(w); // triggers error
}
以上产生以下错误
error: call of overloaded ‘func(const Wrap<A::Widget>&)’ is ambiguous
func(t);
~~~~^~~
note: candidate: void B::func(const T&) [with T = Wrap<A::Widget>]
void func(const T&) { }
^~~~
note: candidate: void A::func(const T&) [with T = Wrap<A::Widget>]
void func(const T&) { }
^~~~
如果Wrap
位于全局命名空间中,为什么会考虑A::func
?B::caller
不应该打电话给B::func
吗?
ADL 不仅在模板的情况下考虑函数的参数。在这里,您将Wrap<A::Widget>
作为B::caller
的参数。因为caller
在namespace B
,B::func
显然是被考虑在内。考虑A::func
的原因来自以下几点(着重号是后加的)
N659 6.4.2/(2.2) [basic.lookup.argdep]
如果 T 是类类型(包括联合),则其关联的类为:类本身;它所在的类 成员(如有);及其直接和间接基类。其关联的命名空间是最里面的 包含其关联类的命名空间。此外,如果 T 是类模板专用化, 其关联的命名空间和类还包括:与 为模板类型参数提供的模板参数的类型[...]
因为A::Widget
是要Wrap
的模板参数,所以A
也是Wrap<A::Widget>
的关联命名空间
此示例可以通过使用限定名阻止 ADL 来按预期进行编译:
template <typename T>
void caller(const T& t) {
B::func(t);
}
或者将函数名称括在 parethes 中
template <typename T>
void caller(const T& t) {
(func)(t);
}
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 考虑模板参数的参数相关外观 (ADL)?