3 ms·
Originally C++ lacked templates, and even once it got templates it was 20 (?) years before it got variadic templates. Operator overloads were added early, howe
by phaedrus 7y ago
Originally C++ lacked templates, and even once it got templates it was 20 (?) years before it got variadic templates. Operator overloads were added early, however, so using an operator for this was a "hack" to make a type safe print function. (It probably also served as a demo application for operator overloading.)
If you squint enough, it looks like a fluent interface, just fluent interfaces wouldn't become mainstream until later. That is, "cout << a << b << c;" is pretty close to "cout.print(a).print(b).print(c);"
Another reason to use an operator is that if you use free functions (without variadic parameters) then your parens and nesting stacks up:
"print(print(print(cout, a), b), c);"
An infix operator doesn't have that problem.
In modern C++, there is at least the possibility to replace "<<" with something better for printing.
- rand84545 7y agoI mean, it's also looks intuitive. if you imagine the arrows as the data fllowing.