Topic: Candidate sets
Author: Marcelo Cantos <marcelo@mds.rmit.edu.au>
Date: 1997/12/03 Raw View
I am confused about the behaviour of some code:
template <class T>
bool operator!=(const T& lhs, const T& rhs) { return !(lhs == rhs); }
int main()
{
enum B {b} x = b;
return x != b;
}
According to the standard, operator!=() cannot be invoked on a local
enum or class. Consequently, operator!=(x, b) is illegal. What I am
not clear on is whether the compiler should produce and error, or use
the built-in != operator after implicitly casting x and b to integers.
I would imagine that the latter behaviour is more intuitive and
convenient, though my reading of the standard gives me the impression
that it should do the former (operator!=() belongs in the candidate
set and is, by definition [though not intuitively], viable).
The reason this came up is that SunPro and gcc accept the above code,
while Visual C++ doesn't (and, FWIW, egcs breaks).
Thanks in advance,
Marcelo
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/12/03 Raw View
Marcelo Cantos <marcelo@mds.rmit.edu.au> writes:
>I am confused about the behaviour of some code:
>
> template <class T>
> bool operator!=(const T& lhs, const T& rhs) { return !(lhs == rhs); }
>
> int main()
> {
> enum B {b} x = b;
> return x != b;
> }
>
>According to the standard, operator!=() cannot be invoked on a local
>enum or class. Consequently, operator!=(x, b) is illegal. What I am
>not clear on is whether the compiler should produce and error, or use
>the built-in != operator after implicitly casting x and b to integers.
The former.
>I would imagine that the latter behaviour is more intuitive and
>convenient, though my reading of the standard gives me the impression
>that it should do the former (operator!=() belongs in the candidate
>set and is, by definition [though not intuitively], viable).
Your reading of the draft standard is correct.
My intuition doesn't agree with yours. I think the behaviour specified
in the draft is in general more intuitive, and much safer, than
choosing a second-best match if the best match happens to be ill-formed.
In this specific case, the semantics of the two operators -- the
user-defined template and the builtin -- are identical, and so
it doesn't matter which one is chosen, and so I suppose you could
argue that would be more convenient for the compiler to pick the
second-best match in this case. However, that's not true in general.
In the general case the compiler has no way of knowing whether the
semantics of different overloaded functions will be the same, and
usually they won't be.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/12/04 Raw View
Fergus Henderson wrote:
>
> Marcelo Cantos <marcelo@mds.rmit.edu.au> writes:
>
> >I am confused about the behaviour of some code:
> >
> > template <class T>
> > bool operator!=(const T& lhs, const T& rhs) { return !(lhs == rhs); }
> >
> > int main()
> > {
> > enum B {b} x = b;
> > return x != b;
> > }
> >
> >According to the standard, operator!=() cannot be invoked on a local
> >enum or class. Consequently, operator!=(x, b) is illegal. What I am
> >not clear on is whether the compiler should produce and error, or use
> >the built-in != operator after implicitly casting x and b to integers.
Good question !
> The former.
No. While your annalysis is correct, the data on which it's
based is outdated.
The built-in comparison on enums bool operator!= (B, B) is
selected here, as it's an exact match and a non-template
function (see the FDIS).
> My intuition doesn't agree with yours.
But there is no problem.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "V.A.Neelesh" <v_a_neelesh@hotmail.com>
Date: 1997/12/05 Raw View
Hi,
It is corrert that '!=' here, cannot be initialized with locally
declared enumeration. Know the question is whether the compiler should
generate an error or should call the built-in !=.
What i think is that compiler should Select the built-in '!=' coz while
looking for the best match it can come to know that the template version
of '!=' cannot be called so select the appropriate one(i.e built in). I
don't think there is nay problem with this.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/12/05 Raw View
V.A.Neelesh wrote:
> It is corrert that '!=' here, cannot be initialized with locally
> declared enumeration. Know the question is whether the compiler should
> generate an error or should call the built-in !=.
>
> What i think is that compiler should Select the built-in '!='
No, selecting the built-in operator!= (int, int) when there
is a better match without a diagnostic would be silly, but
the best match is operator!= (enum, enum) which is selected
(this is an _exact_ match).
So the compiler should not generate an error, not call
the built-in operator!= (int, int), but call annother
built-in operator.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "V.A.Neelesh" <v_a_neelesh@hotmail.com>
Date: 1997/12/05 Raw View
> No, selecting the built-in operator!= (int, int) when there
> is a better match without a diagnostic would be silly, but
> the best match is operator!= (enum, enum) which is selected
> (this is an _exact_ match).
Neelesh -Compiler will never select the built in operator !=(int, int)
when a better viable function !=(enum, enum) is available. The question
was whethet the compiler should select the builtin !=(enum,enum) or
generate an error(which MS Visual C+= 5.0 does), coz template function
cannot be called.
According to 13.3.2.10 - Viable function F1 is defined to be better than
viable function F2 if F1 is a non- template function and F2 is a
template function specialization.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]