1

I know that SGI STL is as old as myself, but I still want to figure it out.

In stl_stack.h, there is some code like this:

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;
}

In stl_config.h, __STL_NULL_TMPL_ARGS is defined like this:

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

but when I tried to compile it with G++ 4.9.2, the compilers said this:

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&);

I don't know why the exactly same code can't compile on my computer, is this code illegal code now or something?

Thank you very much!!!

Jiahao Cai
  • 1,222
  • 1
  • 11
  • 25

1 Answers1

1

stl_stack.h has an error which must have gone under the radar of contemporary compilers. It's illegal to mention operator== <> before operator== has been declared as a template.

To fix this, declare operator== before the definition of stack. But if you're declaring it at that point, you might as well define it. And the declaration will require a forward declaration of stack:

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
…

(Of course, having added the operator== definition to the top, you'll remove it from the bottom.)

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421