Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

basic operator lookup question

1 view
Skip to first unread message

b166e...@gmail.com

unread,
Jul 25, 2008, 8:28:01 PM7/25/08
to
Hello,

I have a basic operator lookup question. In the following case, why is
version 1 picked up?

Am I right in thinking that both operators will be found in the
initial candidate set and then version 1 is picked from this set,
probably because members are preferred?

I am trying to familiarize myself to the process of reading the
standard and I could not yet find the relevant clause that would
explain this..

Thanks!
Mayuresh.


----------------------
class C
{
public:
int operator+ (int) //1
{}
};

int operator+ (const C&, int) //2
{}

C c;
int x = c + 1 //Always picks version 1

int main()
{}
----------------------


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Thomas Maeder

unread,
Jul 26, 2008, 3:27:50 PM7/26/08
to
b166e...@gmail.com writes:

> I have a basic operator lookup question. In the following case, why
> is version 1 picked up?
>
> Am I right in thinking that both operators will be found in the
> initial candidate set

Yes.


> and then version 1 is picked from this set, probably because members
> are preferred?

No.

1 is picked because it is better match. 2 involves a conversion from
non-const to non-const, while 1 is a perfect match.


If you declare 1 const, you ought to get a compiler error, because the
two candidates are now equally good matches.

Go on and remove const from the first parameter of 2, and 2 will be
picked.

Jiang

unread,
Jul 27, 2008, 5:24:05 PM7/27/08
to

> I have a basic operator lookup question. In the following case, why is
> version 1 picked up?
>

[...]


> I am trying to familiarize myself to the process of reading the
> standard and I could not yet find the relevant clause that would
> explain this..

If you want to verify Thomas Maeder's words using the standard,
then please check 13.3.3.

Given these definitions, a viable function F1 is defined to be a
better function than another viable function F2 if for all
arguments i, ICSi(F1) is not a worse conversion sequence
than ICSi(F2), and then

— for some argument j, ICSj(F1) is a better conversion
sequence than ICSj(F2), or, if not that,
— F1 is a non-template function and F2 is a function
template specialization, or, if not that,
— F1 and F2 are function template specializations, and
the function template for F1 is more specialized
than the template for F2 according to the partial
ordering rules described in 14.5.5.2, or, if not that,
— the context is an initialization by user-defined
conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the
standard conversion sequence from the return type
of F1 to the destination type (i.e., the type of the
entity being initialized) is a better conversion
sequence than the standard conversion sequence
from the return type of F2 to the destination type.

For your example, conversion sequence dominates the
best viable function selection. And note member/free
function is not an issue here.

HTH

Jiang

0 new messages