0
我有一個模板專門化類,我需要聲明一個函數模板作爲這個類的朋友。我已經創建了下面的代碼,這些代碼在MSVC編譯器上編譯和運行良好,但它不適用於代碼戰士編譯器。爲了使它在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);
*/
};
全碼:
#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);
}
它是最小的例子嗎? – 2010-12-16 13:52:53
我們需要一個較小的代碼示例。 – Puppy 2010-12-16 17:04:09