开发者

A quick question on stack impl of C++ standard library

What does the line:

template<typename _Tp1, typename _Seq1>
friend开发者_StackOverflow bool
operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);

in http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.4/a01367.html

do?

Why is _Tp1 repeated twice in arguements list? Thanks,


That's like asking why in:

int strcmp( const char * a, const char * b );

const char * is repeated twice - there are two things to compare. The _Tp1 template parameter is the type of thing being stored in the stack - both stacks being compared must store the same type.

Please note that reading the Standard Library source is not a good way of learning C++ - you need a good book, such as this one.


It declares the equality operator between two stacks a friend function of this class, which is necessary for it to access private members.

The const stack<_Tp1, _Seq1> appear twice because there are 2 arguments.

Of course it can be written as

bool operator==(const stack<_Tp1, _Seq1>& y) const { return c == y.c; }

but the C++ standard (§[stack.ops] (23.3.5.3.4)) seems to require this operator to be a free function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜