Re: operator+ overload resolution

From: Andrey Tarasevich (andreytarasevich_at_hotmail.com)
Date: 07/09/04

  • Next message: Ioannis Vranos: "Re: C++/CLI standard"
    Date: Fri, 09 Jul 2004 00:16:33 -0700
    
    

    Mark wrote:
    >>
    >>According to 13.3.1.2/3, the set of member candidates in this case is
    >>created by qualified lookup for 'C2::operator+'. But this lookup will
    >>not find the inherited 'C1::operator+' in 'C2' simply because it is
    >>hidden in 'C2'. I.e. 'C1::operator+' is not even considered as a viable
    >>function. For this reason there's no ambiguity.
    >>
    >>The potential for ambiguity will appear if you add a 'using' declaration
    >>of 'C1::operator+' to 'C2's definition
    >>
    >> using C1::operator+;
    >>
    >>In this case 'C1::operator+' will be found by name lookup and added to
    >>the set of viable functions. Calling this function will require two
    >>implicit conversions of 'reference binding' type (see 13.3.3.1.4): one
    >>for the implied object argument and another for the explicit argument of
    >>'C1::operator+'. Both bind a 'C1&' to an object of 'C2' type. To me it
    >>looks like it should result in an ambiguity. Such bindings are given the
    >>'Conversion' rank, which means that 'C1::operator+' requires worse
    >>conversion sequence for the implied object argument than 'C2::operator+'
    >>(which requires no conversion). At the same time 'C2::operator+'
    >>requires worse conversion sequence (user-defined) for the explicit
    >>argument than 'C1::operator+'.
    >>
    > My standard is not currently is not within arm's reach, nonetheless
    > I'm not sure if I'm following the meaning behind 'worse conversion
    > sequence' for the implied and the explict object.
    >
    > For C2 worse than C1 (the explicit argument), the fact that the OP
    > only defined operator+ with an int parameter, required a casts of o2
    > to an int to perform the addition. Yes/No? If yes, how's C2 worse
    > than C1?

    'C2::operator+' is worse than 'C1::operator+' because in case of
    'C2::operator+' a conversion from explicit argument type 'C2' to
    explicit parameter type 'int' is required. This can only be done by a
    user-defined conversion in this case.

    In case of 'C1::operator+' another conversion is required: from explicit
    argument type 'C2' to explicit parameter type 'C1&'. As far as I
    understand the standard, this conversion is treated as a standard one
    (even though it is not included in the list of "official" standard
    conversions). For this reason 'C1::operator+' is "better" than
    'C2::operator+' (standard conversions are always "better" than
    user-defined ones).

    > For C1 worse than C2, perhaps I'm misunderstanding the meaning of
    > 'implied object argument'. Elaborate if you will.

    Candidate member functions of class 'T' are considered to have an extra
    parameter referred to as 'implied object parameter'. This parameter has
    type 'T&' (or 'const/volatile T&' for const/volatile member functions).
    This parameter is bound to the object, for which the member function has
    been invoked. (That's what I referred to as 'implied object argument'.
    Not a good choice of words. The argument is not really 'implied'. I
    should've called it simply 'object argument'). Conversions that are
    required in order to bind this parameter are also taken into account by
    overload resolution. Once again, if I understood the standard correctly,
    in this case the implied object parameter for 'C1::operator+' has type
    'C1&' and the object has type 'C2', i.e. a conversion from 'C2' to 'C1&'
    is required (ranked as 'Conversion'). The implied object parameter for
    'C2::operator+' has type 'C2&' and and the object has type 'C2', i.e. no
    conversion is required (ranked as 'Exact match'). That's why
    'C2::operator+' is "better" from this point of view ('Exact
    match'-ranked conversions are always "better" than 'Conversion'-ranked
    ones).

    It possible that the above explanation contains some mistakes caused by
    my misunderstanding of some portions of the standard. Comeau's behavior
    also suggests that. It would be great to get to the bottom of this issue.

    --
    Best regards,
    Andrey Tarasevich
    

  • Next message: Ioannis Vranos: "Re: C++/CLI standard"

    Relevant Pages

    • Re: Casting double to int produces inconsistent results from VS 2003 to VS 2008
      ... Some things in the standard are ... 4.8p1 An rvalue of floating point type can be converted to an rvalue of ... another floating point type. ... the result of the conversion is ...
      (microsoft.public.vc.language)
    • Re: casting constant value from float to unsigned short - compiler bugs?
      ... the conversion that sparked this thread. ... floating to integer types "truncates towards zero". ... Since the C standard provides no ... truncation clause clarifies which one of the multiple definitions ...
      (comp.lang.c)
    • Re: Converting fixed-to-free format
      ... just not source code which is defined as ending at 72. ... The question is whether something is "standard conforming". ... Besides, Fortran compilers are normally full of extensions, which makes it more valuable to understand the numerous type of non-conformance. ... is to say that a conversion utility will convert standard code only, ...
      (comp.lang.fortran)
    • Re: bug in visual studio .net 2003 - breakpoints and memcpy
      ... In any case I think arguing about the standard is besides the point. ... prescribe that the conversion must work. ... > A function pointer may have a special format on some architectures ... second restriction on IA64. ...
      (microsoft.public.win32.programmer.kernel)
    • Re: ifstream bug in VS2005 Beta2 & Platform SDK
      ... One of the options is such that it retains maximum precision on conversion. ... It is like this because the Games developers don't care for exactitude on ... standard, so I figured that's why MSVC didn't use it. ...
      (microsoft.public.vc.stl)