Connecting Tech Pros Worldwide Help | Site Map

function template specialization ambiguity

Rafal Dabrowa
Guest
 
Posts: n/a
#1: Jul 22 '05
Consider the following code:
template <class T>
void f(T, int) {}
template <class T>
void f(int, T) {}
template<>
void f(int, int) {} // ambiguous ?

My compiler complains that the last definition is an "ambiguous template
specialization `f<>' for `void f(int, int)' ". I have tested this on a
few compilers and all they complain there. What is wrong in this
specialization ? Consider similar code:
template <class T, class C>
void f(int, T, C) {}
template <class T, class C>
void f(T, int, C) {}
template<class C>
void f(int, int, C) {}

Compiler does not complain that 3rd function is ambiguous. Why ? What is
the difference ?


Rafal
--
sed -e s/s/a/g <my_address >my_right_address
Rob Williscroft
Guest
 
Posts: n/a
#2: Jul 22 '05

re: function template specialization ambiguity


Rafal Dabrowa wrote in news:co5fi9$847$1@news.onet.pl in comp.lang.c++:
[color=blue]
> Consider the following code:
> template <class T>
> void f(T, int) {}
> template <class T>
> void f(int, T) {}
> template<>
> void f(int, int) {} // ambiguous ?
>
> My compiler complains that the last definition is an "ambiguous
> template
> specialization `f<>' for `void f(int, int)' ". I have tested this on a
> few compilers and all they complain there. What is wrong in this
> specialization ? Consider similar code:
> template <class T, class C>
> void f(int, T, C) {}
> template <class T, class C>
> void f(T, int, C) {}
> template<class C>
> void f(int, int, C) {}
>
> Compiler does not complain that 3rd function is ambiguous. Why ? What
> is the difference ?
>[/color]

The diffrence is that in the second example you aren't
specializing anything, you are just providing an overload
that happens to be a template.

In the first example the explicit specialization f<>(int, int)
could be a specialization of either of the two overloads that
preceded it, hence the ambiguity.

HTH.
[color=blue]
> --
> sed -e s/s/a/g <my_address >my_right_address
>[/color]



Rob.
--
http://www.victim-prime.dsl.pipex.com/
Closed Thread