为什么 ostream::operator<< char 参数的全局函数?
根据 http://www.cplusplus.com/reference/iostream/ ostream/operator%3C%3C/ 运算符<<例如,streambuf 上定义的方法是 ostream 的成员,但对于 char / char * 来说,它是一个全局函数。这种差异背后的设计决策是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
streambuf*
的operator<<
(或int
听起来更简单)和char
可能都是作为会员运营商或非会员(免费)运营商实施。我的猜测是,这是由于在定义 C++ 时出现了向后兼容性问题:也许较旧的代码正在依赖成员
operator<<(int)
,因此他们决定不将其移动为一个免费的运营商。C++ 标准库(还有 STL)有许多像这样的不均匀性。
operator<<
forstreambuf*
(orint
which sounds a simpler case) andchar
could have been both implemented as member operators, or as non-member (free) operators.My guess is that it's due to retro-compatibility issues emerged while C++ was being defined: maybe older code was relaying on a member
operator<<(int)
, and thus they decided not to move it as a free operator.The C++ standard library (and also STL) have a number of dishomogeneity like this one.