C++ 是否可以使模板函数成为模板专门化类的朋友?

C++ 是否可以使模板函数成为模板专门化类的朋友?,c++,visual-c++,templates,codewarrior,C++,Visual C++,Templates,Codewarrior,我有一个模板专门化类,我需要声明一个函数模板作为这个类的朋友。我已经创建了以下代码,这些代码在MSVC编译器上编译并运行良好,但在CodeWarrior编译器上不起作用。为了在codewarrior编译器上工作,我必须取消对模板专门化类中显式声明的注释 这是codewarrior编译器的问题还是代码的某些问题 减少代码大小以提供上下文: //template class template<class R> class Alice { public: A

我有一个模板专门化类,我需要声明一个函数模板作为这个类的朋友。我已经创建了以下代码,这些代码在MSVC编译器上编译并运行良好,但在CodeWarrior编译器上不起作用。为了在codewarrior编译器上工作,我必须取消对模板专门化类中显式声明的注释

这是codewarrior编译器的问题还是代码的某些问题

减少代码大小以提供上下文:

//template class
template<class R>
class Alice
{
    public:
            Alice() {}

    private:
        R review;

    template< class F>
    friend void Watch(F, Movie::Wonderland< Alice<R> >&, int);

};

//template specialization class
template<>
class Alice<void>
{
    public:
            Alice() {}

    private:
        int review;

    template<class F>
    friend void Watch(F, Movie::Wonderland< Alice< void > >&, int);

    /*
    //explicit declaration
    //need to uncomment this to compile on codewarrior 
    //as the function template above doesn't work.
    friend void Watch<void (*)()>(void (*)(), Movie::Wonderland<Alice<void> > &, int);
    */
};
//模板类
模板
爱丽丝班
{
公众:
爱丽丝(){}
私人:
R审查;
模板
朋友虚空手表(F,电影::仙境&,int);
};
//模板专门化类
模板
爱丽丝班
{
公众:
爱丽丝(){}
私人:
国际审查;
模板
friend void Watch(F,电影::仙境>&,int);
/*
//明确声明
//需要取消注释才能在codewarrior上编译
//因为上面的函数模板不起作用。
朋友虚空手表(虚空(*),电影::仙境和,国际);
*/
};
完整代码:

#define ONCE 1
#define NULL
namespace Movie{
template<class C>
class Wonderland
{
    public:
        Wonderland():who(NULL){}
        Wonderland(C* she):who(she){}
        void Attach(C *she)
        {who = she;}
        C* operator->()
        {return who;}
    private:
        C* who;
};
}
//fwd declarations
template<class R> class Alice;
void Watch(Movie::Wonderland< Alice<void> >& theatre, int price);
template<class F> void Watch(F func, Movie::Wonderland< Alice<void> >& theatre, int price);
template<class P, class F> void Watch(F func, P food, Movie::Wonderland< Alice<void> >& theatre, int price);
struct popcorn;

template<class R>
class Alice
{
    public:
            Alice() {}

    private:
        R review;

    friend void Watch(Movie::Wonderland< Alice<R> >&, int);

    template< class F>
    friend void Watch(F, Movie::Wonderland< Alice<R> >&, int);

    template<class P, class F>
    friend void Watch(F, P, Movie::Wonderland< Alice<R> >&, int);
};

template<>
class Alice<void>
{
    public:
            Alice() {}

    private:
        int review;

    friend void Watch(Movie::Wonderland< Alice< void > >&, int);

    template<class F>
    friend void Watch(F, Movie::Wonderland< Alice< void > >&, int);

    template<class P, class F>
    friend void Watch(F, P, Movie::Wonderland< Alice< void > >&, int);

    /*
    //explicit declarations
    friend void Watch(Movie::Wonderland<Alice<void> > &, int);
    friend void Watch<void (*)()>(void (*)(), Movie::Wonderland<Alice<void> > &, int);
    friend void Watch<void (*)(), void (*)()>(void (*)(), void (*)(), Movie::Wonderland<Alice<void> > &, int);
    friend void Watch<popcorn, void (*)()>(void (*)(), popcorn, Movie::Wonderland<Alice<void> > &, int);
    */
};

//template<class R>
void Watch(Movie::Wonderland< Alice<void> >& theatre, int price) 
{
    theatre.Attach(new Alice<void>);
    int review = theatre->review;
    return;
}

template<class F>
void Watch(F func, Movie::Wonderland< Alice<void> >& theatre, int price) 
{
    theatre.Attach(new Alice<void>);
    int review = theatre->review;
    return;
}

template<class P, class F>
void Watch(F func, P food, Movie::Wonderland< Alice< void > >& theatre, int price)
{
    theatre.Attach(new Alice<void>);
    int review = theatre->review;
    return;
}

void goWatch(void)
{
    return;
}

void eatPopcorn(void)
{
    return;
}

struct popcorn
{

};

int main()
{
    struct popcorn sweetPopcorn;
    Movie::Wonderland< Alice<void> > theatre;
    Watch(goWatch, theatre, ONCE);
    Watch(goWatch, eatPopcorn, theatre, ONCE);
    Watch(theatre, ONCE);
    Watch(goWatch, sweetPopcorn, theatre, ONCE);
}
#定义一次1
#定义空值
名称空间电影{
模板
班级仙境
{
公众:
仙境():谁(空){}
仙境(C*she):谁(她){}
无效附加(C*she)
{谁=她;}
C*运算符->()
{返回who;}
私人:
C*世卫组织;
};
}
//fwd声明
模板类Alice;
void Watch(电影:仙境和剧院,国际价格);
模板无效手表(F func,电影::仙境&剧院,国际价格);
模板无效手表(F func、P食品、电影::仙境和剧院,国际价格);
结构爆米花;
模板
爱丽丝班
{
公众:
爱丽丝(){}
私人:
R审查;
friend void Watch(电影:仙境&,int);
模板
朋友虚空手表(F,电影::仙境&,int);
模板
朋友虚空手表(F,P,电影::仙境&,int);
};
模板
爱丽丝班
{
公众:
爱丽丝(){}
私人:
国际审查;
friend void Watch(电影:仙境>&,int);
模板
friend void Watch(F,电影::仙境>&,int);
模板
朋友虚空手表(F,P,电影::仙境&,int);
/*
//显式声明
朋友虚空手表(电影::仙境和int);
朋友虚空手表(虚空(*),电影::仙境和,国际);
朋友虚空手表(虚空(*)(),虚空(*)(),电影::仙境和,国际);
朋友void手表(void(*)(),爆米花,电影::仙境和,int);
*/
};
//模板
void Watch(电影:仙境和剧院,国际价格)
{
剧院附设(新爱丽丝);
int review=剧院->回顾;
返回;
}
模板
void Watch(F func,电影::仙境&剧院,国际价格)
{
剧院附设(新爱丽丝);
int review=剧院->回顾;
返回;
}
模板
void Watch(F func,P食品,电影::仙境>&剧院,国际价格)
{
剧院附设(新爱丽丝);
int review=剧院->回顾;
返回;
}
void goWatch(void)
{
返回;
}
void吃爆米花(void)
{
返回;
}
结构爆米花
{
};
int main()
{
结构爆米花甜爆米花;
电影:仙境剧院;
观看(goWatch,剧院,一次);
观看(goWatch,EatPopmorn,剧院,一次);
观看(戏剧一次);
手表(goWatch,甜爆米花,剧院,一次);
}

我已经检查了您的代码,并用两个编译器对其进行了测试:g++-4.2和clang++。我认为您的好友声明没有任何问题。

我们需要一个较小的代码示例。