尾随返回类型中的名称查找问题
Name lookup issue in trailing return type
下面的例子说明了我的问题:
#include <iostream>
#include <string>
template <typename T>
auto func(const T& x) -> decltype(to_string(x)) {
using std::to_string;
return to_string(x);
}
int main() {
std::cout << func(1);
}
我不想将std::to_string
导入全局命名空间,也不想使用-> decltype(std::to_string(x))
,因为这样做会禁用 ADL。显然,你不能把using std::to_string
放在decltype
之内。那么,我应该怎么做呢?
遵从另一个命名空间;
namespace activate_adl {
using std::to_string;
template <typename T>
auto func(const T& x) -> decltype(to_string(x)) {
return to_string(x);
}
}
template <typename T>
auto func(const T& x) -> decltype(activate_adl::func(x)) {
return activate_dl:: func(x);
}
这允许 ADL 仍然可以完成,但不会污染全局命名空间。
在一些与std
相关的函数和 ADL 中多次遇到此问题后,我发现延迟命名空间(名称良好)是合适的替代方案。
相关文章:
- 算法问题:查找从堆栈中弹出的所有序列
- 在 for 循环中查找问题时遇到困难
- 介绍类 在C++中查找圆半径的问题
- 查找不等式为真的次数时出现问题
- Distirbute Candy - 查找问题的最小可重现示例
- 查找在 4 和问题中代码失败的测试用例
- 在文本中查找半个单词"ABBA"的问题
- 查找有问题的宏
- 使用用户定义的函数查找数字的幂时出现问题
- 使用二分法查找数的平方根时出现问题
- 如何解决非限定名称查找问题
- 使用STD :: MAP在数据及其性能问题中查找重复项.我可以预先分配吗?
- 在二叉树中插入/查找问题
- 显式模板专用化和依赖名称查找问题
- 函数模板查找问题
- 字符串::查找问题 (C++)
- 尾随返回类型中的名称查找问题
- 在共享对象中命名不兼容的符号-在哪里查找问题
- 在DirectShow筛选器图中使用Async_reader和Wave Parser会导致视频查找问题
- 在字符串中查找问题的c++脚本