From 3762869960175860072
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,cbf1693e6ece180b
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2003-12-15 01:43:15 PST
Path: archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!news-out.visi.com!petbe.visi.com!newsfeed.esat.net!colt.net!kibo.news.demon.net!mutlu.news.demon.net!demon!mail2news.demon.co.uk!devnull
From: rani_sharoni@hotmail.com ("Rani Sharoni")
Newsgroups: comp.std.c++
Subject: Re: template friends
Date: Mon, 15 Dec 2003 09:43:13 +0000 (UTC)
Lines: 84
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <3fdd802f$1@news.microsoft.com>
References: <4MpCb.70557$Qn2.26313@newssvr25.news.prodigy.com>
X-Trace: mail2news.demon.co.uk 1071481393 29521 10.0.0.1 (15 Dec 2003 09:43:13 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Mon, 15 Dec 2003 09:43:13 +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 1AVpFv-0007g0-00
	for mail2news@news.news.demon.net; Mon, 15 Dec 2003 09:43:12 +0000
X-Received: from localhost (localhost [[UNIX: localhost]]) by mulga.cs.mu.OZ.AU
	id UAA14033; Mon, 15 Dec 2003 20:43:05 +1100 (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-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
X-Original-NNTP-Posting-Host: ranis20.middleeast.corp.microsoft.com
X-OriginalArrivalTime: 15 Dec 2003 09:33:24.0626 (UTC) FILETIME=[7CAD8F20:01C3C2EE]
X-Spam-Status: No, hits=-3.2 required=5.0
	tests=AWL,BAYES_01,EMAIL_ATTRIBUTION,HTML_10_20,PRIORITY_NO_NAME,
	      REFERENCES,SEMIFORGED_HOTMAIL_RCVD
	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++:683

"Carl Daniel" wrote:
> Consider the following code:
>
> // <code>
> template <class T> class C;
>
> template <class T1, class T2> T1 f(const C<T2>&); // 1
> template <class T> T f(const C<T>&); // 2
> [...]
> // </code>
>
> [...]
> Is Comeau right?  (It usually is!)

// My previous response was lost but maybe this time it will find its way.

AFAICT friends are not the issue here.
The problems that you encountered boils down to the question whether the
following code contains ambiguity:

template<typename T> struct A { A(int = 0); };

template<typename T1, typename T2> // #1
T1 f(const A<T2>&) { return T1(); }

template<typename T1>  // #2
T1 f(const A<T1>&)
{
    typedef typename T1::force_error type;
    return T1();
}

int x1 = f<int>(A<int>());

There are two non-ordered specializations candidates:
int f<int, int>(const A<int>&); // from #1
int f<int>(const A<int>&);       // from #2

Both candidates generates the *same* type and therefore it seems that the
code contains an ambiguity. EDG and GCC found the code well-formed which
proves that #1 was selected. VC found an ambiguity.

There is subtle different between the two candidates since #2 contain
non-deduced context (i.e. the template argument was explicitly specified). I
replaced #2 argument with true non-deduced argument (i.e. identity template
below) and all compilers agreed that the code is ambiguous which shows that
EDG and GCC are a bit inconsistent.

Anyway, In case that EDG and GCC are right then here is an interesting
technique that allows conversions when calling function template which might
be useful for user defined operators (i.e. operator+(complex, complex)):

template<typename T> struct identity { typedef T type; };

template<typename T>
char* g(const A<T>&, const A<T>&)
{ return 0; }

template<typename T>
long* g(const A<T>&, typename identity<const A<T>&>::type)
{ return 0; }

template<typename T>
int*  g(typename identity<const A<T>&>::type, const A<T>&)
{ return 0; }

A<int> a(10);

char* x2 = g(a, a);
long* x3 = g(a, 10); // allow conversion
int*  x4 = g(10, a); // allow conversion

EDG and GCC compiled the code while VC found an ambiguity.

Rani



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



