By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

template function specialization query

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.