1,为什么STL stack的pop操作返回值类型是void,而不直接返回栈顶元素,用一个pop同时搞定top和pop两个函数的工作岂不是更加优雅高效?
SGI的解释:
One might wonder why pop() returns void, instead of value_type. That is, why must one usetop() andpop() to examine and remove the top element, instead of combining the two in a single member function? In fact, there is a good reason for this design. Ifpop() returned the top element, it would have to return by value rather than by reference: return by reference would create a dangling pointer. Return by value, however, is inefficient: it involves at least one redundant copy constructor call. Since it is impossible forpop() to return a value in such a way as to be both efficient and correct, it is more sensible for it to return no value at all and to require clients to usetop() to inspect the value at the top of the stack.
简言之,当pop返回时,根据语义,栈顶元素已经出栈了,此时不能再返回一个引用,而必须将栈顶元素以传值的方式返回,需要至少构造一个临时对象,影响效率。虽然有NRV优化的存在,但是一方面不能完全依赖编译器,另一方面NRV优化在Stan Lippman看来必须要有Copy constructor存在。
另一个观点是,如果stack中存放的是复杂的类型,在copy constructor的过程中发生异常,则临时对象就会丢失,这牵涉到thread safety的一些讨论,关于此问题的一些相关信息:
http://www.sgi.com/tech/stl/stack.html
http://www.gotw.ca/gotw/008.htm
2,关于itoa函数的设计问题。首先给出itoa的原型:
char *_itoa(
int value,
char *str,
int radix
);
为什么要在两个地方返回结果?其实最后一个参数str既是传入参数也是传出参数,它指向的是调用者预分配好的空间,itoa假定这个空间足够大(这也就是为啥这个函数不安全的原因,假如str指向的空间不够大,就溢出了,所以_itoa_s需要强制指定str指向的空间大小,并在函数内部检查确保不溢出)。
之所以将结果字符串同时也通过返回值返回,我猜测是便于方便这样调用:strlen(_itoa(100, str, 10)),可以直接将函数的返回值用作其他字符串函数的参数。