简体   繁体   中英

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

What does the line:

template<typename _Tp1, typename _Seq1>
friend 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 stack sa 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM