function non-template versus fully specialized function template

  • Thread starter Robert Allan Schwartz
  • Start date
R

Robert Allan Schwartz

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
 
V

Victor Bazarov

Robert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top