C++ 尾随返回类型中的名称查找问题
下面的例子说明了我的问题:C++ 尾随返回类型中的名称查找问题,c++,c++14,template-meta-programming,argument-dependent-lookup,trailing-return-type,C++,C++14,Template Meta Programming,Argument Dependent Lookup,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); } #包括
#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);
}
#包括
#包括
模板
自动函数(常量T&x)->decltype(到字符串(x)){
使用std::to_字符串;
返回到_字符串(x);
}
int main(){
std::cout遵从另一个名称空间
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);
}
名称空间激活\u adl{
使用std::to_字符串;
模板
自动函数(常量T&x)->decltype(到字符串(x)){
返回到_字符串(x);
}
}
模板
自动函数(常量T&x)->取消类型(激活adl::函数(x)){
返回activate_dl::func(x);
}
这允许ADL仍然可以完成,但不会污染全局名称空间
在使用一些std
相关函数和ADL多次遇到这个问题后,我找到了延迟名称空间(名称良好)是合适的替代品。标记中有C++14。如果使用C++14,则根本不需要尾部返回类型。只需保留将禁用SFINAE的auto
@Revolver\u Ocelot,如果不需要SFINAE,(在这种特定情况下),可以使用std::string
作为返回类型。