Topic: Is this correct compiler behvior? -- it is not


Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/09/16
Raw View
Gene Bushuyev wrote:

> Valentin Bonnard wrote in message <37DFCEC8.6217@wanadoo.fr>...
> [snip]
> >Candidates are
> >
> >Function                 Conversion rank          Template
> >specialisation
> >S::operator== (const S&) exact match on all args  non template
> >::operator== (S,S)       exact match on all args  template
> >
> >The non-template function shall be prefered.
> >
>
> IMHO, non-template function (bool operator == (const S &s) const ) requires
> cv-qualification conversion,

No, it's a direct binding. Note that previously (before London),
S::operator== (const S&) was even prefered because it doesn't
involve a lvalue-to-rvalue conversion !

I guess many compilers implement the pre-London rule.

It would be a cv-qualification conversion if we had a pointer
argument:

Signatures in scope:
 foo (const int*)
 template <typename T> void foo (T*)

int i;
foo (&i);

calls foo (int*) (from templated foo (T*)) because of the
qualification ajustement required to call foo (const int*).

--

Valentin Bonnard


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/09/15
Raw View
biwi wrote:

>         template <typename T>
>         bool operator == (const T, const T)

const is irrelevant here

> MSVC generates calls to this operator in situations in which I'm not
> sure it should generate them. Example of two such situations is:
>
>         enum E { E1, E2 };
>
>         struct S
>         {
>           bool operator == (const S &s) const { return true; }
>         };
>
>         void main (void)
>         {
>           E e = E1;
>           S s;
>
>           if (e == E1) ;   // template operator == is invoked
>           if (s == S ()) ; // template operator == is invoked

Candidates are

Function                 Conversion rank          Template
specialisation
S::operator== (const S&) exact match on all args  non template
::operator== (S,S)       exact match on all args  template

The non-template function shall be prefered.

Should call: S::operator== (const S&)

>         }
>
> For the enum, it seems like enum types do not have operator ==.

There is now one, but it's a pretty new rule (London/June 97 I think),
that many compilers probably haven't implemented yet.

Candidates are:

Function              Where from        Conversion rank         Template
specialisation
::operator== (E, E)   template function exact match on all args template
::operator== (E, E)   pseudo prototype  exact match on all args non
template
::operator== (int,int)pseudo prototype  promotion on all args   non
template
(there are many others candidates: ::operator== (int,unsigned int),
::operator== (int,long), and about 33 others)

So the prefered function is:
::operator== (E, E)   pseudo prototype

and you function should never be called. In theory, you
don't even have to give it a body for the program to link.

--

Valentin Bonnard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Gene Bushuyev" <gbush@my-deja.com>
Date: 1999/09/16
Raw View
Valentin Bonnard wrote in message <37DFCEC8.6217@wanadoo.fr>...
[snip]
>Candidates are
>
>Function                 Conversion rank          Template
>specialisation
>S::operator== (const S&) exact match on all args  non template
>::operator== (S,S)       exact match on all args  template
>
>The non-template function shall be prefered.
>

IMHO, non-template function (bool operator == (const S &s) const ) requires
cv-qualification conversion, while template function - doesn't. Based on
that, I believe 13.3.3 specifies that the template function is better viable
function. All compilers I tried do exactly that.

>> For the enum, it seems like enum types do not have operator ==.
>
>There is now one, but it's a pretty new rule (London/June 97 I think),
>that many compilers probably haven't implemented yet.


That's right. BCB does exactly that, while other compilers I tried (gcc
2.8.1 and SC5) incorrectly call template function.

Gene Bushuyev




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]