Discussion:
Operator ==() for fundamental types.
(too old to reply)
Vladimir Grigoriev
2010-01-25 17:53:25 UTC
Permalink
May I write operator ==( 10, 20 ) ?

Vladimir Grigoriev
Victor Bazarov
2010-01-25 18:20:40 UTC
Permalink
Post by Vladimir Grigoriev
May I write operator ==( 10, 20 ) ?
No, the Standard explicitly prohibits overloading operators for built-in
types (Standard, [Expr]/3). Since 10 and 20 are of built-in type, int,
you can't define an operator that would potentially alter the behaviour
of the equality operator defined by the Standard.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Stephan T. Lavavej [MSFT]
2010-01-25 20:22:05 UTC
Permalink
That's why you can't define operator==(int, int). The reason why you can't
call operator==(10, 20) is different.

C++03 13.6 [over.built]/1:

"The candidate operator functions that represent the built-in operators
defined in clause 5 are specified in
this subclause. These candidate functions participate in the operator
overload resolution process as
described in 13.3.1.2 and are used for no other purpose. [Note: because
built-in operators take only
operands with non-class type, and operator overload resolution occurs only
when an operand expression
originally has class or enumeration type, operator overload resolution can
resolve to a built-in operator only
when an operand has a class type that has a user-defined conversion to a
non-class type appropriate for the
operator, or when an operand has an enumeration type that can be converted
to a type appropriate for the
operator."

STL
Post by Victor Bazarov
Post by Vladimir Grigoriev
May I write operator ==( 10, 20 ) ?
No, the Standard explicitly prohibits overloading operators for built-in
types (Standard, [Expr]/3). Since 10 and 20 are of built-in type, int,
you can't define an operator that would potentially alter the behaviour of
the equality operator defined by the Standard.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Vladimir Grigoriev
2010-01-26 15:28:13 UTC
Permalink
In the "C++ in a nutshell" of Ray Lischner is written

You can use the function notation with
built-in operators, too, but such usage is extremely uncommon. For example:
operator-(42, 10) // Same as 42 - 10
operator-(33) // Same as -33

Vladimir Grigoriev
Post by Stephan T. Lavavej [MSFT]
That's why you can't define operator==(int, int). The reason why you
can't call operator==(10, 20) is different.
"The candidate operator functions that represent the built-in operators
defined in clause 5 are specified in
this subclause. These candidate functions participate in the operator
overload resolution process as
described in 13.3.1.2 and are used for no other purpose. [Note: because
built-in operators take only
operands with non-class type, and operator overload resolution occurs only
when an operand expression
originally has class or enumeration type, operator overload resolution can
resolve to a built-in operator only
when an operand has a class type that has a user-defined conversion to a
non-class type appropriate for the
operator, or when an operand has an enumeration type that can be converted
to a type appropriate for the
operator."
STL
Post by Victor Bazarov
Post by Vladimir Grigoriev
May I write operator ==( 10, 20 ) ?
No, the Standard explicitly prohibits overloading operators for built-in
types (Standard, [Expr]/3). Since 10 and 20 are of built-in type, int,
you can't define an operator that would potentially alter the behaviour
of the equality operator defined by the Standard.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Igor Tandetnik
2010-01-26 16:23:52 UTC
Permalink
Post by Vladimir Grigoriev
In the "C++ in a nutshell" of Ray Lischner is written
You can use the function notation with
built-in operators, too, but such usage is extremely uncommon. For
example: operator-(42, 10) // Same as 42 - 10
operator-(33) // Same as -33
Ray Lischner is wrong. Consider getting a better textbook.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Continue reading on narkive:
Loading...