473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

template function specialization query

Hi,

If I do this:

void
func(const int& i) {...}

template<class T>
func(const T& i) {...}

Is this equivalent to, or different to, the following?

template<class T>
func(const T& i) {...}

template<>
void
func<int>(const int& i) {...}

--
http://www.it-is-truth.org/
Jul 22 '05 #1
2 1087
On Wed, 12 May 2004 16:34:04 +0100, Asfand Yar Qazi
<im_not_giving_it_here@i_hate_spam.com> wrote:
Hi,

If I do this:

void
func(const int& i) {...}

template<class T>
func(const T& i) {...}

Is this equivalent to, or different to, the following?

template<class T>
func(const T& i) {...}

template<>
void
func<int>(const int& i) {...}


Different, but you have to stress it to make the difference show up.
Consider this:

#include <iostream>
using namespace std;

void func1(const int& i) { cout << "func1(const int &)" << endl;}

template<class T>
void func1(const T& i) { cout << "func1(const T &) [template]" << endl;}

// Is this equivalent to, or different to, the following?

template<class T>
void func2(const T& i) { cout << "func2(const T &) [template]" << endl;}

template<>
void func2<int>(const int& i) { cout << "func2(const int&) [template]" <<
endl;}

int main()
{
cout << "Using ints:" << endl;
func1(5);
func2(5);

cout << endl;

cout << "Using longs:" << endl;
func1(5L);
func2(5L);
return 0;
}

Output:
Using ints:
func1(const int &)
func2(const int&) [template]

Using longs:
func1(const T &) [template]
func2(const T &) [template]

The difference is in the overload resolution rules. Using ints, the best
match is the completely-ordinary non-template.

Using longs, an exact match of a template specialization beats using a
non-template where a conversion has to happen.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Leor Zolman wrote:
<snip>
The difference is in the overload resolution rules. Using ints, the best
match is the completely-ordinary non-template.

Using longs, an exact match of a template specialization beats using a
non-template where a conversion has to happen.
-leor


Ahhhh.....
--
http://www.it-is-truth.org/
Jul 22 '05 #3

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
8
by: mattias.nissler | last post by:
Hi! Here is a problem I ran into at work. The following example doesn't compile on gcc-4.1: struct cons_end {}; template<typename U,typename Vstruct cons { U elem; V tail;
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.