Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > function non-template versus fully specialized function template

Reply
Thread Tools

function non-template versus fully specialized function template

 
 
Robert Allan Schwartz
Guest
Posts: n/a
 
      08-09-2004
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-09-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inability to explicitly specialize a template class within another non-specialized template class jn@ngedit.com C++ 1 02-17-2006 07:17 PM
specialized member function takes precedence over generic template member function bluekite2000@gmail.com C++ 1 07-20-2005 08:58 PM
Out-of-class definition of a non-template member of an explicitly specialized class template? BigMan C++ 3 04-22-2005 06:28 AM
fully specialized function template and "multiple definition" Hartmut Sbosny C++ 2 04-20-2005 04:51 PM
template template argument from specialized instance gao_bolin@voila.fr C++ 0 03-29-2005 05:36 PM



Advertisments