Question

Acording to http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/ the operator<< method defined on e.g. streambuf is a member of ostream, but for char / char * it is a global function. What's the design decision behind this difference?

Was it helpful?

Solution

operator<< for streambuf* (or int which sounds a simpler case) and char 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top