0

説明は非常に気が遠くなる可能性があるので、私は例にまっすぐに行きます:

#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>友達になりましょう。 fr<bool>::callsomeone<int>fr<bool>::callsomeone<char>

必要に応じて、c++11を頼りにできます。

4

1 に答える 1

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

関数テンプレートの特殊化ではなく、関数テンプレートと友達になりたい(つまり、すべての特殊化と友達になりたい)ので、テンプレート引数をcallsomeoneに渡してはなりません。

于 2012-04-09T20:14:01.937 に答える