Topic: disambiguation rules (was: Deprecate the use of plain pointers as standard container iterators)
Author: wkaras@yahoo.com
Date: Fri, 5 May 2006 21:43:00 CST Raw View
kuyper@wizard.net wrote:
> Nicola Musatti wrote:
.
> > The following code gives different results on different platforms:
> >
> > #include <vector>
> > #include <algorithm>
> > #include <iostream>
> >
> > int * find(int * b, int * e, int d) {
> > return b + d < e ? b + d : e;
> > }
> >
> > int main() {
> > std::vector<int> v;
> > v.push_back(3);
> > v.push_back(2);
> > v.push_back(1);
> > std::vector<int>::iterator i = find(v.begin(), v.end(), 2);
> > std::cout << ( i != v.end() ? *i : 0 ) << '\n';
> > }
> >
> > If std::vector<>::iterator is a pointer the user defined find function
> > is chosen.
>
> "Doctor, it hurts when I hit myself in the head with a hammer."
> "Then stop hitting yourself in the head with a hammer."
>
> Use std::find(), if you want to make sure that it's actually
> std::find() that you're calling. If you actually want the compiler to
> consider other possible overloads, make sure that any other overload
> that could be selected has acceptable semantics, and doesn't lead to
> ambiguity as to which overload should be selected. This is the same
> advice that applies whenever calling any function; there's nothing
> special about the fact that v.begin() might (or might not) be a
> pointer.
.
In this example, it's clear that it would be better if the function
from the namespace implied by the parameter took precedence
over the function in the namespace of the call. What is the
rational for the order of precedence?
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: "Nicola Musatti" <nicola.musatti@gmail.com>
Date: Mon, 8 May 2006 11:24:28 CST Raw View
wkaras@yahoo.com wrote:
> kuyper@wizard.net wrote:
> > Nicola Musatti wrote:
[...]
> > > int * find(int * b, int * e, int d) {
> > > return b + d < e ? b + d : e;
> > > }
> > >
> > > int main() {
> > > std::vector<int> v;
// ...
> > > std::vector<int>::iterator i = find(v.begin(), v.end(), 2);
> > > std::cout << ( i != v.end() ? *i : 0 ) << '\n';
> > > }
> > >
> > > If std::vector<>::iterator is a pointer the user defined find function
> > > is chosen.
[...]
> In this example, it's clear that it would be better if the function
> from the namespace implied by the parameter took precedence
> over the function in the namespace of the call. What is the
> rational for the order of precedence?
Non templated functions always take precedence over templated ones,
which in general it is reasonable as the former can be seen as
conceptual specializations of the latter. In fact the real problem of
my (contrived) example is the fact that my own find is misnomed.
It is still an example of the traps laid out for the unaware within the
most basic elements of the standard library.
Cheers,
Nicola Musatti
---
[ 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://www.comeaucomputing.com/csc/faq.html ]