简体   繁体   English

关于C ++标准库的堆栈展示的一个快速问题

[英]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 http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.4/a01367.html中

do? 做?

Why is _Tp1 repeated twice in arguements list? 为什么_Tp1在争论列表中重复两次? 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. const char *重复两次-有两件事需要比较。 The _Tp1 template parameter is the type of thing being stored in the stack - both stacks being compared must store the same type. _Tp1模板参数是存储在堆栈中的事物的类型-被比较的两个堆栈必须存储相同的类型。

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 . 请注意,阅读标准库源不是学习C ++的好方法-你需要一本好书,比如这一个

It declares the equality operator between two stack sa friend function of this class, which is necessary for it to access private members. 它在该类的两个stack 朋友函数之间声明相等运算符,这对于访问私有成员是必需的。

The const stack<_Tp1, _Seq1> appear twice because there are 2 arguments. const stack<_Tp1, _Seq1>出现两次,因为有2个参数。

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. 但是C ++标准(§[stack.ops](23.3.5.3.4))似乎要求此运算符为自由函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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