2010-12-16 5 views
0

템플릿 특수화 클래스가 있고이 클래스의 친구로 함수 템플릿을 선언해야합니다. MSVC 컴파일러에서 컴파일 및 작동하는 다음 코드를 이미 작성했지만 코드 전사 컴파일러에서는 작동하지 않습니다. 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); 
} 
+0

최소한의 예입니까? –

+1

작은 코드 예제가 필요합니다. – Puppy

답변

1

를, 2 개의 컴파일러에 대해 그것을 테스트 : g ++ - 4.2 연타 ++. 친구 선언과 관련하여 어떤 문제도 보이지 않습니다.

관련 문제