473,804 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_addre ss
Jul 22 '05 #1
1 2464
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_addre ss


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2354
by: Ivan | last post by:
How (at compile time) can one determine whether some class implements a particular member function? Name only is sufficient, full signature match nice but not required. The application is a template class that (among other things) defines an iterator class. The category of iterator depends on the template argument type: if the argument defines only operator++ then the iterator will be a forward_iterator, but if it also defines operator--...
4
6490
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
7
5076
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone offered me a kind explanation. TIA Function template and specialization below works fine: template <typename T> T maximum (T a, T b) {
2
5781
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
5
6590
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
12
2983
by: RA Scheltema | last post by:
Hi all, I have the following code: namespace A { inline void func(int) { ...; } inline void func(float) { ...; } inline void func(char) { ...; } }
6
2767
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave no errors compiling this:
5
1656
by: desktop | last post by:
I have this example: template<class T(1) void f( T ); template<class T(2) void f( T* ); template< (3)
13
6601
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.