S
Stefan Ram
The output operator "<<" for operands of type
»::std:stream« and »char« (in this order) seems to be
defined as a non-member, while for »double« it seems to be
defined as a member.
Why is this so?
Bjarne Stroustrup already explained some of this: He said
that "<<" /can/ be defined as a non-member, because the
class already has the member »put«, so it can be defined
outside of the class using »put«. But why do we /prefer/ this?
Herb Sutter explained, why "<<" for his class »Complex« has
to be defined as a non-member of this class »Complex«, but
his reason does not apply to the class »::std:stream« as
far as I understand it.
Sometimes people say: When you define the operators as
non-members and the class has implicit conversion operators,
those implicit conversions will behave more symmetric with
respect to the two operands of a binary operator, when this
operator is defined as a non-member. I can not understand
all of this, because I lack some prerequisites, but I have a
vague idea of what it might mean. Does this reason apply to
»::std:stream« and "<< static_cast<char>(...)"?
Could someone give a specific example of a case where
something would be worse when "<< static_cast<char>(...)"
would have been defined as a member of ::std:stream?
Or, an example of a case where something would be worse when
"<< static_cast<double>(...)" would have been defined as a
non-member of ::std:stream?
(This question is inspired by a similar question posted into
the German language C++ newsgroup by someone else recently.)
»::std:stream« and »char« (in this order) seems to be
defined as a non-member, while for »double« it seems to be
defined as a member.
Why is this so?
Bjarne Stroustrup already explained some of this: He said
that "<<" /can/ be defined as a non-member, because the
class already has the member »put«, so it can be defined
outside of the class using »put«. But why do we /prefer/ this?
Herb Sutter explained, why "<<" for his class »Complex« has
to be defined as a non-member of this class »Complex«, but
his reason does not apply to the class »::std:stream« as
far as I understand it.
Sometimes people say: When you define the operators as
non-members and the class has implicit conversion operators,
those implicit conversions will behave more symmetric with
respect to the two operands of a binary operator, when this
operator is defined as a non-member. I can not understand
all of this, because I lack some prerequisites, but I have a
vague idea of what it might mean. Does this reason apply to
»::std:stream« and "<< static_cast<char>(...)"?
Could someone give a specific example of a case where
something would be worse when "<< static_cast<char>(...)"
would have been defined as a member of ::std:stream?
Or, an example of a case where something would be worse when
"<< static_cast<double>(...)" would have been defined as a
non-member of ::std:stream?
(This question is inspired by a similar question posted into
the German language C++ newsgroup by someone else recently.)