c++ - 如何为非类型模板参数类型考虑 ADL
问题描述
我想在函数调用中查找期间考虑非类型模板参数的命名空间中的函数。
例如:
template<auto& N>
struct test_nontype {
int call() {
return f(*this); // error: 'f' was not declared in this scope
}
};
template<typename T>
struct test_type {
int call() {
return f(*this); // ok, f is found in ns via ADL
}
};
namespace ns {
static struct type_in_ns {} value;
int f(test_nontype<value>) {
return 3;
}
int f(test_type<type_in_ns&>) {
return -3;
}
}
int main() {
int x = test_type<ns::type_in_ns&>{}.call();
return x + test_nontype<ns::value>{}.call();
}
使用类型它工作正常,但最后一行无法编译,因为int ns::f(test_nontype<ns::value>)
找不到。
template<auto& N, typename = decltype(N)> struct test_nontype {
如果有替代品,我宁愿不添加参数,因为添加模板参数只会使界面更加混乱(我必须添加注释来解释它是用于 ADL,但 IDE 仍然建议添加类型名称,并且它还会增加更多的噪音来编译错误)
还有哪些其他解决方法?
解决方案
推荐阅读
- python - 二叉树Python的最小深度不起作用
- python - 如何使用 python 脚本 ssh 到远程 linux 服务器,并能够在 python 脚本中使用 ssh 时以用户指定的身份执行命令
- javascript - 生命周期钩子中的声明方法与VueJs中的方法属性有什么区别
- python - 如何在 Python 中创建和遍历决策树
- java - 访问另一个类中的树形图并遍历项目
- redis - 如何扩展 Redis 队列
- python - 根据另一列中的值选择组
- android - 我应该如何连接 Room、LiveData 和 ContentProvider?
- python-2.7 - Django API 调用有时返回 404 错误,多次返回 200 ok 。URL 存在于应用程序中
- ios - 使用 self.navigationController?.pushViewController(newViewController, animated: true) 的问题