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

function non-template versus fully specialized function template

P: n/a
Starting with:

// a primary function template:
template <typename T>
inline
void dumb(T & x)
{
x = 1;
}

What is the difference between:

// A:

// a fully specialized function template:
template <>
inline
void dumb(int & x)
{
x = 2;
}

and:

// B:

// a function non-template:
inline
void dumb(int & x)
{
x = 2;
}

What would make me choose A versus B, or vice versa?

Thanks for your help!

Robert Schwartz
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Robert Allan Schwartz wrote:
Starting with:

// a primary function template:
template <typename T>
inline
void dumb(T & x)
{
x = 1;
}

What is the difference between:

// A:

// a fully specialized function template:
template <>
inline
void dumb(int & x)
{
x = 2;
}

and:

// B:

// a function non-template:
inline
void dumb(int & x)
{
x = 2;
}

What would make me choose A versus B, or vice versa?


It most cases, if the function _is_ used, there is no real difference.
However, the template will not be instantiated if it's not used, while
a regular function might (even if it's declared 'inline'). Modern
linkers often discard unused libraries from the final application when
linking the object modules, but it's not a feature of the language.

Victor
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.