gpt4 book ai didi

c++ - 模板类中模板方法的友元,在模板类中

转载 作者:行者123 更新时间:2023-11-28 08:07:12 25 4
gpt4 key购买 nike

描述可能令人难以置信,所以我直接看例子:

#include <iostream>
#include <typeinfo>
using namespace std;

template<typename T> class fr{
static void privatestuff(){
cout<<"private of "<<typeid(T).name()<<endl;
}
public:
template<typename TT> void callsomeone(){
fr<TT>::privatestuff();
}
//template<typename TT> friend void fr<TT>::callsomeone<T>();
//template<> template<typename TT> friend void fr<TT>::callsomeone<T>();
//template<typename TT> template<> friend void fr<TT>::callsomeone<T>();
//no other combinations... how to get it?
};

int main(){
fr<bool> obj;
obj.callsomeone<int>();
}

基本上,我希望 fr 能够调用 fr<int>::privatestuff .但我也不想公开超出需要的内容,所以制作 fr<int> 只有的 friend fr<bool>::callsomeone<int> , 不是 fr<bool>::callsomeone<char>或其他人。

如果需要的话,我可以依靠 c++11。

最佳答案

template<class x> template<class y> friend void fr<x>::callsomeone();

你不应该将任何模板参数传递给 callsomeone,因为你想要友好一个函数模板,而不是它的特化(换句话说,你想友好它的所有特化)。

关于c++ - 模板类中模板方法的友元,在模板类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com