From 2493217278321676949
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,3b5faa9aaea18095
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2003-10-01 08:53:54 PST
Path: archiver1.google.com!news2.google.com!newsfeed.stanford.edu!newsfeed.berkeley.edu!ucberkeley!kibo.news.demon.net!mutlu.news.demon.net!demon!mail2news.demon.co.uk!devnull
From: Patrick.Kowalzick@cern.ch ("Patrick Kowalzick")
Newsgroups: comp.std.c++
Subject: Re: Problem with function template overload resolution
Date: Wed, 1 Oct 2003 15:53:53 +0000 (UTC)
Organization: CERN
Lines: 143
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <bleao6$kke$1@sunnews.cern.ch>
References: <4e6fccc6.0309162227.1aa4bda7@posting.google.com> <3F6E975F.8050300@grad.hr> <bkos2l$8t1$1@sunnews.cern.ch> <4e6fccc6.0309231302.4c6a4a9f@posting.google.com> <fur9nvsta83c5et8drf2amoqhhs1lvoqu5@4ax.com> <blcbkh$k0u$1@sunnews.cern.ch>
X-Trace: mail2news.demon.co.uk 1065023633 13661 10.0.0.1 (1 Oct 2003 15:53:53 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Wed, 1 Oct 2003 15:53:53 +0000 (UTC)
X-Received: from mulga.cs.mu.oz.au ([128.250.1.22])
	by news.demon.co.uk with esmtp (Exim 4.12)
	id 1A4jIV-0003YC-00
	for mail2news@news.news.demon.net; Wed, 01 Oct 2003 15:53:52 +0000
X-Received: from localhost (localhost [[UNIX: localhost]]) by mulga.cs.mu.OZ.AU
	id BAA19771; Thu, 2 Oct 2003 01:53:47 +1000 (EST)
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Path: comp-std-cpp-robomod!not-for-mail
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Delivered-To: std-c++@ucar.edu
X-Newsgroups: comp.std.c++
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
X-Spam-Status: No, hits=-5.1 required=5.0
	tests=BAYES_10,HTML_SHOUTING5,QUOTED_EMAIL_TEXT,REFERENCES
	version=2.55
X-Spam-Checker-Version: SpamAssassin 2.55 (1.174.2.19-2003-05-19-exp)
Xref: archiver1.google.com comp.std.c++:12

Hello Graeme,

I think I did not express me correct , yesterday. Sorry for that. I do not
doubt your explanation of the standard, but I have some problems
understanding it.

My aim here is to understand the behaviour of code. But our example here is
a very tough one, and I bet if you ask very experienced programmers you get
a bunch of diferent answers.

This is a reason why I "do not like" your answer, even if I could follow. If
I knew how to determine which function is called with a call, I would need
several hours to get the clue for a 20 lines program. And afterwards I would
not be sure if I did it correct.

> > Partial ordering applied to these two functions works like this ...
> >
> > template<typename A>
> > result<A, typename A::value_type> foo(A const& a) {
> >    std::cout << "foo<A>" << std::endl;
> >    return result<A, typename A::value_type>();
> > }
> >
> > template<typename R, typename A>
> > R foo(A const& a) {
> >    std::cout << "foo<R, A>" << std::endl;
> >    return R();
> > }
> >
> >
> > For the first foo(), synthesize a unique type U for the template
> > type parameter A and call the second foo  i.e. the call is foo( abc
> > ) where abc is U const &.  For this call, the compiler is unable to
> > deduce the type of the template parameter R so deduction fails.
> >
> > Now this is done in reverse  - for the second foo(), synthesize
> > unique types U1, U2 for R and A and call the first foo()  - the call
> > is
> > foo(abc) where abc has type U1 const &.  Deduction succeeds because the
> > template parameter A of the first function is deduced as U1.  Hence the
> > second foo is more specialised than the first (para 4 & 5)  so the call
> > foo<arg2<int> >(s2); is not ambiguous.

OK, I agree but I try to construct a new example, where the first foo
accepts only a special type. (without any interpretation: bcc32 and g++ do
not compile, MSCV does)

// ***** CODE starts *****
#include <iostream>

class arg_1 {};
class arg_2 {};

template<typename A> void foo(const arg_1&)
{
   std::cout << "foo<A>" << std::endl;
}

template<typename R, typename A > void foo(const A&)
{
   std::cout << "foo<R, A>" << std::endl;
}


int main() {

 foo < arg_1 > ( arg_1() );
 foo < arg_2 > ( arg_2() );
 foo < arg_1 > ( arg_2() );
 foo < arg_2 > ( arg_1() );

 foo< arg_1 , arg_1 > ( arg_1() );
 foo< arg_2 , arg_1 > ( arg_1() );

}

// ***** CODE ends *****

Now I try to follow the logic to determine the specialization: -first foo()
is taking U, calls second with foo(U) which fails because there is no
possibility to deduct the second type. -second foo() is taking U, calls
first with foo(U) which fails because arg_1 is expected.

This means, neither first nor second foo is more specialized and the call
foo<U>(arg_1()) may not work.
(I hope I am right until here otherwise do not read the rest ;-) , which
means g++ and bcc32 are perfectly right as well)

But me personally would assume the same rules for overloading like the ones
for not templated functions and just take the first one, because arg_1 is
the parameter which is passed.
I found this in
"14.8.3 Overload Resolution
[snip]
The complete set of candidate functions includes all the function templates
instantiated in this way ann all of the non-template overload functions of
the same name. The function template specializations are treated like any
other functions in the remainder of overload resolution,..."

I personally would assume a call to the first foo() for each call
foo<U>(arg_1()); with the definition of the overload resolution.
(which MSVC does) and for the second with foo<U>(arg_2()); which is
absolutly clear.
Anyway I am really confused with all the different expressions used and I am
sure I mixed them all up.

> Here I do not agree. Part 2 says:
>
> Given two overloaded function templates, wheter one is more
> specialized than another can be determined by transforming each
> template in turn and using argument deduction (14.8.2) to compare it
> to each other.
>
> 1. foo() is not overloaded
> 2. foo<U> is overloaded
> 3. foo<U1,U2> is not overloaded.
>
> So I would use only case 2 which results in same specializaion level.
> So the code could not be compiled.

Here I mean:
I do not understand why to compare the calls foo(U) if the calls foo<U>(U)
are causing troubles. Would it be more clear if it is
"Giving two overloaded function template SPECIALIZATIONS...."
? In the sense of
"14.8
Aa function instantiated from a function template is called a function
template specialization;"
Or is this completly misinterpreted?

I am really confused, could not follow the logic and decided myself to keep
the fingers away from constructions like that ;-).

Thanks a lot for your time,
Patrick


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]



