Topic: Is this correct compiler behvior?


Author: biwi <info@biwi.ee.ethz.ch>
Date: 1999/09/15
Raw View
Hi.

As some implementaions of STL require operator == (and operator <) be
defined on any type put into containers (even if they are never sorted),
I've defined universal template for operator ==, which should be never
executed at runtime.

 #include <assert.h>

 template <typename T>
 bool operator == (const T, const T) { assert (false); return false; }

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
 }

For the enum, it seems like enum types do not have operator ==. May be
they are converted to ints for comparisons purposes, and such conversion
might be worse than template instantiation.

And for the struct, declaring the template operator== with reference
parameters, ie (const T&, const T&), helps.
So it seems that conversion value -> reference is worse than template
instantiation.

So my question is if such behavior conforms C++ stadard (any).

[ moderator's note: there is only one C++ standard. Refer to the
  FAQ below. -sdc ]

Thanks,
Stepan


[ 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: Ron Natalie <ron@sensor.com>
Date: 1999/09/15
Raw View
biwi wrote:
>
> For the enum, it seems like enum types do not have operator ==. May be
> they are converted to ints for comparisons purposes, and such conversion
> might be worse than template instantiation.

Enums can have operator== overloaded.  Invocation of your templated
overload is what is expected.

>
> And for the struct, declaring the template operator== with reference
> parameters, ie (const T&, const T&), helps.
> So it seems that conversion value -> reference is worse than template
> instantiation.

Helps what?  It looks to me like you have an ambiguous set of overloads
in this case.  But I'm sure someone else will figure out why the template
is preferred.
---
[ 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: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/16
Raw View
On 15 Sep 1999 16:01:48 GMT, biwi <info@biwi.ee.ethz.ch> wrote:

>As some implementaions of STL require operator == (and operator <) be
>defined on any type put into containers (even if they are never sorted),

Note that this is a bug in MSVC.  The standard does not require every
type used in a standard container to define an operator== or operator<.

The principle behind this is -- functions of a template function are
instantiated only if they are used.  So if you never use the function
   bool vector<SomeClass>::operator==(const vector<SomeClass>&) const;
then the compiler should never instantiate this function.  Hence it
will never complain about a missing operator==(SomeClass,SomeClass).

This rule is most useful, because it extends greatly the types T
that you can use in any standard or non-standard container.  Another
application of this rule is that you can have a container of
objects that lack a default constructor!




>I've defined universal template for operator ==, which should be never
>executed at runtime.
>
> #include <assert.h>
>
> template <typename T>
> bool operator == (const T, const T) { assert (false); return false; }
>
>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
> }

The first one is legal.
>   if (e == E1) ;   // template operator == is invoked
This calls ::operator==(T,T) with T==::E.

The second one seems like it should call ::S::operator==(const S&) const
and not the template version.

--
--------------
siemel b naran
--------------


[ 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              ]