Topic: Partial ordering with return types


Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/05/26
Raw View
Jaakko J   rvi wrote:
>
> Chris Heath wrote:
>
> > template <class R, class T> R foo(T p) {}
> >
> > template <class R, class T> R foo(T* p) {}
> >
> > int main () {
> >    int i;
> >    foo<double>(&i);
> > }
> > ----------------------
> >
> > My compiler (egcs) will not compile this, complaining that the call to foo
> > is ambiguous.  They claim that the second foo is NOT more specialized than
> > the first.
>
> Yes, I noticed the same. This seems strange, particularly since in
>
>  template <class T> void foo(T p) {}
>  template <class T> void foo(T* p) {}
>   ...
>  int i;
>  foo<double>(&i);
>
> the deduction succeeds and the latter is chosen.

Of course here there is no deduction !

> > Is it really not allowed by the standard?
> AFAIK no. see 14.5.5.2. example 6.

You must mean yes (it is allowed) here. (Never
use negation in a question !)

--

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: Jaakko =?iso-8859-1?Q?J=E4rvi?= <jaakko.jarvi@cs.utu.fi>
Date: 1999/05/27
Raw View
Oops, I meant:
>=20
> Yes, I noticed the same. This seems strange, particularly since in
>=20
>  template <class T> void foo(T p) {}
>  template <class T> void foo(T* p) {}
>   ...
>  int i;
>  foo(&i);
   --------

/Jaakko

--=20
--- Jaakko J=E4rvi, jaakko.jarvi@cs.utu.fi
--- Turku Centre for Computer Science (www.tucs.fi)
--- Lemmink=E4isenkatu 14 A, FIN-20520 Turku, Finland
--- Phone: +358-2-333 8656, Fax: +358-2-333 8600
---
[ 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: Chris Heath <cheath@math.lsa.umich.edu>
Date: 1999/05/24
Raw View
I am wondering if this code is legal C++.  The issue is whether the two
function templates foo are partially ordered or not.

----------------------
// general version of function template foo:
template <class R, class T> R foo(T p) {}

//more specialized version (or is it?!!)
template <class R, class T> R foo(T* p) {}

int main () {
   int i;
   foo<double>(&i);
}
----------------------

My compiler (egcs) will not compile this, complaining that the call to foo
is ambiguous.  They claim that the second foo is NOT more specialized than
the first.

When I ran across this problem in my program, I was very surprised.
Intuitively speaking, it seems that the second foo should be more
specialized than the first, so foo<double>(&i) should unambiguously call
the second foo.

Is it really not allowed by the standard?

Chris Heath
---
[ 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: Jaakko =?iso-8859-1?Q?J=E4rvi?= <jaakko.jarvi@cs.utu.fi>
Date: 1999/05/25
Raw View
Chris Heath wrote:

> template <class R, class T> R foo(T p) {}
>
> template <class R, class T> R foo(T* p) {}
>
> int main () {
>    int i;
>    foo<double>(&i);
> }
> ----------------------
>
> My compiler (egcs) will not compile this, complaining that the call to foo
> is ambiguous.  They claim that the second foo is NOT more specialized than
> the first.

Yes, I noticed the same. This seems strange, particularly since in

 template <class T> void foo(T p) {}
 template <class T> void foo(T* p) {}
  ...
 int i;
 foo<double>(&i);

the deduction succeeds and the latter is chosen.
Also KAI C++ eats your code without complaining.

> Is it really not allowed by the standard?
AFAIK no. see 14.5.5.2. example 6.

/Jaakko


PS. I have another related question:

With these defined:

template<class T> void foo(const T& t); // 1
template<class T> void foo(T& t); // 2

int i; const int& j;

Are the following calls ambiguous or not

foo(j); // calls 1?
foo(i); // calls 2?


--
--- Jaakko J   rvi, jaakko.jarvi@cs.utu.fi
--- Turku Centre for Computer Science (www.tucs.fi)
--- Lemmink   isenkatu 14 A, FIN-20520 Turku, Finland
--- Phone: +358-2-333 8656, Fax: +358-2-333 8600


[ 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: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/05/26
Raw View
In article <Pine.SOL.3.96-4hack.990524143214.24015A-
100000@vanceulen.math.lsa.umich.edu>, cheath@math.lsa.umich.edu
says...
> I am wondering if this code is legal C++.  The issue is whether the two
> function templates foo are partially ordered or not.

[ code elided ]

Yes, I believe they are.

> My compiler (egcs) will not compile this, complaining that the call to foo
> is ambiguous.  They claim that the second foo is NOT more specialized than
> the first.

I believe egcs is incorrect about the ordering issue, but the code
still shouldn't compile.  As-is, you've said that foo will return a
double, but neither version of foo returns anything...
---
[ 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              ]