469,627 Members | 953 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,627 developers. It's quick & easy.

function template specialization ambiguity

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
Jul 22 '05 #1
1 2228
Rafal Dabrowa wrote in news:co**********@news.onet.pl in comp.lang.c++:
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 ?

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.
--
sed -e s/s/a/g <my_address >my_right_address


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ivan | last post: by
4 posts views Thread by Dave Theese | last post: by
12 posts views Thread by RA Scheltema | last post: by
6 posts views Thread by wkaras | last post: by
5 posts views Thread by desktop | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.