OT Signature quote [was Re: Unrecognized escape sequences in string literals]

Nobody nobody at nowhere.com
Sun Aug 16 22:21:43 EDT 2009


On Sun, 16 Aug 2009 05:05:01 +0000, Steven D'Aprano wrote:

> Now that I understand what the semantics of cout << "Hello world" are, I 
> don't have any problem with it either. It is a bit weird, "Hello world" 
> >> cout would probably be better,

Placing the stream on the LHS allows the main forms of << to be
implemented as methods of the ostream class. C++ only considers the LHS
operand when attempting to resolve an infix operator as a method.

Also, << and >> are left-associative, and that cannot be changed by
overloading. Having the ostream on the LHS allows the operators to be
chained:

	cout << "Hello" << ", " << "world" << endl

equivalent to:

	(((cout << "Hello") << ", ") << "world") << endl

[operator<< returns the ostream as its result.]

Even if you could make >> right-associative, the values would have to be
written right-to-left:

	endl >> "world" >> ", " >> "Hello" >> cout
i.e.:
	endl >> ("world" >> (", " >> ("Hello" >> cout)))




More information about the Python-list mailing list