2017-01-19 1 views
1

SGI STL은 나 자신만큼 오래되었다는 것을 알고 있습니다. 그러나 나는 아직도 그것을 알아 내고 싶습니다.SGI STL의 바운드 친구 템플릿

template <class T, class Sequence = Deque<T> > 
class Stack { 
    friend bool operator== __STL_NULL_TMPL_ARGS (const Stack&, const Stack&); 
    friend bool operator< __STL_NULL_TMPL_ARGS (const Stack&, const Stack&); 
protected: 
    Sequence c; 
}; 

template <class T, class Sequence> 
bool operator== (const Stack<T, Sequence>& x, const Stack<T, Sequence>& y){ 
    return x.c == y.c; 
} 

template <class T, class Sequence> 
bool operator< (const Stack<T, Sequence>& x, const Stack<T, Sequence>& y){ 
    return x.c < y.c; 
} 

stl_config.h에서 __STL_NULL_TMPL_ARGS는 다음과 같이 정의된다 : stl_stack.h에서

는이 같은 일부 코드가

# ifdef __STL_CLASS_PARTIAL_SPECIALIZATION 
# define __STL_TEMPLATE_NULL template<> 
# else 
# define __STL_TEMPLATE_NULL 
# endif 

을하지만 G ++ 4.9.2로 컴파일하려고 할 때, 컴파일러는 다음과 같이 말했다 :

In file included from stack.cpp:1:0: 
stack.h:13:22: error: declaration of ‘operator==’ as non-function 
    friend bool operator== __STL_NULL_TMPL_ARGS (const Stack&, const Stack&); 
        ^
stack.h:13:22: error: expected ‘;’ at end of member declaration 
In file included from iterator.h:3:0, 
       from deque.h:6, 
       from stack.h:5, 
       from stack.cpp:1: 
stl_config.h:111:31: error: expected unqualified-id before ‘<’ token 
# define __STL_NULL_TMPL_ARGS <> 
          ^
stack.h:13:25: note: in expansion of macro ‘__STL_NULL_TMPL_ARGS’ 
    friend bool operator== __STL_NULL_TMPL_ARGS (const Stack&, const Stack&); 

내가 왜 그런지 모르겠다. 내 컴퓨터에서 같은 코드를 컴파일 할 수 없으면이 코드가 현재 불법 코드인가?

대단히 감사합니다!

+0

당신은'찾고 될 수 있습니다 그리고 선언은 stack의 전방 선언이 필요합니다 # define __STL_NULL_TMPL_ARGS <>', line 192. – Potatoswatter

+0

코드가 더 이상 적합하지 않을 수 있지만 질문이 더 많은 템플릿을위한 구문과 관련이 있다면 여기를 살펴보십시오. http://stackoverflow.com/a/35891188/3747990 – Niall

+0

''# define __STL_NULL_TMPL_ARGS <>'''이 포함되어 있습니다. @Potatoswatter – Jiahao

답변

1

stl_stack.h은 현대 컴파일러의 레이다에서 벗어난 오류가 있습니다. operator==이 (가) 템플릿으로 선언되기 전에 operator== <>을 언급하는 것은 불법입니다.

이 문제를 해결하려면 stack 앞에 operator==을 선언하십시오. 그러나 그 시점에서 선언한다면, 정의 할 수도 있습니다. (당신이 바닥에서 제거 할 수 있습니다, 물론, 상단에 operator== 정의를 추가하는 데.)

template <class T, class Sequence> 
class stack; // Forward declare for sake of operator== declaration. 

template <class T, class Sequence> 
bool operator==(const stack<T, Sequence>& x, const stack<T, Sequence>& y) { 
    return x.c == y.c; 
} 

#ifndef __STL_LIMITED_DEFAULT_TEMPLATES 
template <class T, class Sequence = deque<T> > 
#else 
…