Topic: function templates trouble
Author: "Razvan Cojocaru" <razvanco@gmx.net>
Date: Sat, 9 Mar 2002 00:42:54 GMT Raw View
I thank you for your solution but the point was not finding a solution but
whether the code should compile or not considering the ISO C++ rules.
Regards,
Razvan
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: "Razvan Cojocaru" <razvanco@gmx.net>
Date: Thu, 7 Mar 2002 22:04:26 GMT Raw View
Hello.
I've posted this a while back on comp.lang.c++ and I got an answer which I
then thought was absolutely right and made me think it was a stupid
question. Since then I've spoken to more people about it and everyone seems
to have a different opinion on the subject. The code is:
template<class T> void f(T, T) {}
template<class X, class Y>
class Test {};
template<class X, class Y, class Z>
void f(Test<X, Z>, Test<Y, Z>) {}
int main()
{
Test<int, int> t1, t2;
f(t1,t2);
}
Now this code compiles just fine with the Borland C++ 5.5 compiler and
MSVC++ 7, which use f<int,int,int>(). However, gcc-2.95.3 and gcc-3.0.4 and
the Comeau compiler ( I used the online tryout thing ), say that the
overload for f is ambiguous (in this case, where X is the same type as Y).
If anyone could enlighten me please, it would be greatly appreciated. What's
the right behaviour?
Please include the numbers of the sections concerned (of the ISO C++
standard) in your explanations.
Thank you,
Razvan
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: "Anthony Williams"<anthwil@nortelnetworks.com>
Date: Fri, 8 Mar 2002 15:35:34 GMT Raw View
"Razvan Cojocaru" <razvanco@gmx.net> wrote in message
news:a68nnn$cb6l9$1@ID-135723.news.dfncis.de...
> Hello.
>
> I've posted this a while back on comp.lang.c++ and I got an answer which I
> then thought was absolutely right and made me think it was a stupid
> question. Since then I've spoken to more people about it and everyone
seems
> to have a different opinion on the subject. The code is:
>
> template<class T> void f(T, T) {}
>
> template<class X, class Y>
> class Test {};
>
> template<class X, class Y, class Z>
> void f(Test<X, Z>, Test<Y, Z>) {}
>
> int main()
> {
> Test<int, int> t1, t2;
> f(t1,t2);
> }
>
> Now this code compiles just fine with the Borland C++ 5.5 compiler and
> MSVC++ 7, which use f<int,int,int>(). However, gcc-2.95.3 and gcc-3.0.4
and
> the Comeau compiler ( I used the online tryout thing ), say that the
> overload for f is ambiguous (in this case, where X is the same type as Y).
> If anyone could enlighten me please, it would be greatly appreciated.
What's
> the right behaviour?
> Please include the numbers of the sections concerned (of the ISO C++
> standard) in your explanations.
The possible options are f<int,int,int> and f<Test<int,int> >, since both
have the same signature. Which is preferred is determined by the partial
ordering rules in 14.5.5.2. In particular paragraphs 3, 4 and 5.
So we start with f<T>, and substitute a unique type for T (__unique_1__)
(paragraph 3). The call is now f(__unique_1__,__unique_1__), which doesn't
(in general) match f<X,Y,Z>, since __unique_1__ is not of type Test<A,B> for
any A,B. Therefore f<T> is at least as specialized as f<X,Y,Z> (paragraph
4).
Now we try it the other way => f<X,Y,Z> becomes
f(Test<__unique_2__,__unique_4__>,Test<__unique_3__,__unique_4__>). Since
this has different types for the two parameters (__unique_2__ and __unique_3
are distinct, as they are unique types), we cannot deduct T for f<T>, as
both parameters should be the same type. Therefore f<X,Y,Z> is at least as
specialized as f<T>.
Since they are both at least as specialized as the other, neither is more
specialized than the other (paragraph 5).
Therefore neither is better than the other (13.3.3p1), so (13.3.3p2) the
call is ill-formed.
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: "thor" <thor@abcsys.com.pl>
Date: Fri, 8 Mar 2002 09:44:19 CST Raw View
"Razvan Cojocaru" <razvanco@gmx.net> wrote in message
news:a68nnn$cb6l9$1@ID-135723.news.dfncis.de...
> Hello.
>
> I've posted this a while back on comp.lang.c++ and I got an answer which I
> then thought was absolutely right and made me think it was a stupid
> question. Since then I've spoken to more people about it and everyone
seems
> to have a different opinion on the subject. The code is:
>
> template<class T> void f(T, T) {}
>
> template<class X, class Y>
> class Test {};
>
> template<class X, class Y, class Z>
> void f(Test<X, Z>, Test<Y, Z>) {}
>
> int main()
> {
> Test<int, int> t1, t2;
> f(t1,t2);
> }
>
> Now this code compiles just fine with the Borland C++ 5.5 compiler and
> MSVC++ 7, which use f<int,int,int>(). However, gcc-2.95.3 and gcc-3.0.4
and
> the Comeau compiler ( I used the online tryout thing ), say that the
> overload for f is ambiguous (in this case, where X is the same type as Y).
> If anyone could enlighten me please, it would be greatly appreciated.
What's
> the right behaviour?
> Please include the numbers of the sections concerned (of the ISO C++
> standard) in your explanations.
>
> Thank you,
> Razvan
//header aaa.h
template<class T> void f(T, T) {};
template<class X, class Y>
class Test {};
template<class X, class Y, class Z>
void f(X, Y, Z) {f(Test<X, Z>, Test<Y, Z>)};
//body aaa.cpp
#include "aaa.h"
int main()
{
Test<int, int> t1, t2;
f(t1,t2);
}
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: "Philippe A. Bouchard" <philippeb@videotron.ca>
Date: Fri, 8 Mar 2002 16:51:09 GMT Raw View
"Razvan Cojocaru" <razvanco@gmx.net> wrote in message
news:a68nnn$cb6l9$1@ID-135723.news.dfncis.de...
> Hello.
>
> I've posted this a while back on comp.lang.c++ and I got an answer which I
> then thought was absolutely right and made me think it was a stupid
> question. Since then I've spoken to more people about it and everyone
seems
> to have a different opinion on the subject. The code is:
>
> template<class T> void f(T, T) {}
>
> template<class X, class Y>
> class Test {};
>
> template<class X, class Y, class Z>
> void f(Test<X, Z>, Test<Y, Z>) {}
>
> int main()
> {
> Test<int, int> t1, t2;
> f(t1,t2);
> }
How about:
template<class X, class Y>
class Test
{
};
template<class T>
void f(T, T)
{
}
template<class X, class Y, class Z>
void f(Test<X, Z>, Test<Y, Z>)
{
}
int main()
{
Test<int, int> t1, t2;
f<int, int, int>(t1,t2);
}
Philippe A. Bouchard
Fornux Accounting Software
---
[ 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.research.att.com/~austern/csc/faq.html ]