Why is ostream :: operator << a global function for char parameters?

According to http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/ operator <<method defined for example streambuf is a member of ostream, but for char / char * it is a global function. What is the constructive solution to this difference?

+4
source share
1 answer

operator<< for streambuf* (or int , which sounds simpler), and char could be implemented as member operators or as non-member (free) operators.

I assume that this is due to retro-compatibility issues that occurred when defining C ++: perhaps the older code was passed by the operator<<(int) member, and so they decided not to port it as a free operator.

The standard C ++ library (as well as STL) has several heterogeneities like this one.

+3
source

Source: https://habr.com/ru/post/1337597/


All Articles