ADL未找到隐藏的朋友模板功能 [英] Hidden friend template function not found by ADL

查看:62
本文介绍了ADL未找到隐藏的朋友模板功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

namespace Bar {
template<typename...Types>
class Foo {
public:
    template<class T>
    friend T* get_if(Foo<Types...>& f) {
       return nullptr;
    }
};
}
int main() {
  Bar::Foo<int, double> f;
  get_if<int>(f);
  return 0;
}

据我了解,get_if是一个隐藏的朋友,只有ADL才能找到它.但是找不到get_if(至少使用GCC 7.4.1编译),为什么?

From my understanding get_if is an hidden friend and it can be found only by ADL. However get_if is not found (at least compiling with GCC 7.4.1), why?

推荐答案

get_if<int>(f);

问题出在这里,您试图通过指定类型来执行实例化,该类型在知道参数之前需要 get_if< int> 的名称

the problem is here, you tried to perform the instantiation by specifying the type, which needs the name of get_if<int> before the parameter is known

namespace Bar {
template<typename...Types>
class Foo {
public:
    template<class T>
    friend int* get_if(T a, Foo<Types...>& f) {
       return nullptr;
    }
};
}
int main() {
  Bar::Foo<int, double> f;
  get_if(5, f);
  return 0;
}

相反,这可行.

这篇关于ADL未找到隐藏的朋友模板功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆