Skip to main content
gamedev.net
🔒 Locked

templates

Started by Xtreme 18 replies 1k views
Original Post
Xtreme
Xtreme
In C++, do the following templates have the same meaning?

//1. 

template<> void Swap(float x, float y);

//2. or:
template<float> void Swap(float x, float y);

//3. or:
template void Swap<float>(float x, float y);

//4. or:
void Swap(float x, float y);

Whenever I declare it and run the function, i see that 4. is called no matter what. Whats the point of writing unneccessary template keyword in front when you know that 4. will be called all the time instead of the rest?
Yes! There are kangaroos in Australia but I haven't seen them...yet
SiCrane
SiCrane
No they don't have the same meaning. Templates have a strict order of application and qualification. This is a truly obnoxious and arcane corner of the C++ language, so I'll just refer you to this article. Hopefully that will explain the differences.
Fruny
Fruny
When the compiler has the choice between a template version and a non-template version of an overloaded function, it always chooses the non-template version (13.3.3 p220): "... a viable function F1 is defined to be a better function than another viable function F2 if ... F1 is a non-template function and F2 is a template function specialization ..."
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Xtreme
Xtreme
the anon poster was me :)
Yes! There are kangaroos in Australia but I haven't seen them...yet
Fruny
Fruny
Quote:
Original post by Anonymous Poster
IE, whats the point of specialization templates when you know that non-template version will be called instead of specialization templates in overloaded function cases?


But why would you both provide a templated and a non-templated version of the same function, with the same parameters?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Xtreme
Xtreme
Quote:
Original post by Fruny
Quote:
Original post by Anonymous Poster
IE, whats the point of specialization templates when you know that non-template version will be called instead of specialization templates in overloaded function cases?


But why would you both provide a templated and a non-templated version of the same function, with the same parameters?


to check to see which function gets called. My point is it seems if you have a same function with same parameters, it is a waste of declaring template<> in front of the function, since declaring it without template <> is still a specialization!
Yes! There are kangaroos in Australia but I haven't seen them...yet
SiCrane
SiCrane
No, it's not. It's an overload. There's a difference.
Shannon Barber
Shannon Barber
You generally shouldn't overload template functions. You define a generic one and then handle special cases as, well, specializations.

#4 isn't a template
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Xtreme
Xtreme
Quote:
Original post by SiCrane
No, it's not. It's an overload. There's a difference.


So, what you are saying is that say you have the following template declared:
template <typename T> void Swap(T x, T y);


Is the following not an overload?
template <> void Swap(float x, float y);


And the following is?
void Swap(float x, float y);
Yes! There are kangaroos in Australia but I haven't seen them...yet
Xtreme
Xtreme
Quote:
Original post by Magmai Kai Holmlor
You generally shouldn't overload template functions. You define a generic one and then handle special cases as, well, specializations.

#4 isn't a template

Yep. I know #4 isnt but i dont know whats the point of having specialization templates when you can define it like #4.
Yes! There are kangaroos in Australia but I haven't seen them...yet
SiCrane
SiCrane
Quote:
Original post by Xtreme
So, what you are saying is that say you have the following template declared: /snip/


Pretty much. If you call the function through explicit template arguments rather than you'll call the specialization, but if you call it without explicit template arguments then you'll call the overload. Read the article I posted up at the beginning of the thread.
Xtreme
Xtreme
Quote:
Original post by SiCrane
Quote:
Original post by Xtreme
So, what you are saying is that say you have the following template declared: /snip/


Pretty much. If you call the function through explicit template arguments rather than you'll call the specialization, but if you call it without explicit template arguments then you'll call the overload. Read the article I posted up at the beginning of the thread.


thanks. I'm just confused because I do not see the advantages of declaring a specialization template.
Yes! There are kangaroos in Australia but I haven't seen them...yet
Fruny
Fruny
Quote:
Original post by Xtreme
thanks. I'm just confused because I do not see the advantages of declaring a specialization template.


Well, for one thing, you can have partial specializations...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Xtreme
Xtreme
Quote:
Original post by Fruny
Quote:
Original post by Xtreme
thanks. I'm just confused because I do not see the advantages of declaring a specialization template.


Well, for one thing, you can have partial specializations...

and what exactly are partial specializations?
Yes! There are kangaroos in Australia but I haven't seen them...yet
Fruny
Fruny
Quote:
Original post by Xtreme
[nd what exactly are partial specializations?


template<class T, class U> void Foo(T,   U) {}template<class U>          void Foo(int, U) {}


Warning: VC6 doesn't support partial template specialization.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
fallenang3l
fallenang3l
Quote:
Original post by Fruny
template<class T, class U> void Foo(T,   U) {}template<class U>          void Foo(int, U) {}


Warning: VC6 doesn't support partial template specialization.


The C++ standard doesn't support partial specializations for functions.
Teknofreek
Teknofreek
Quote:
Original post by Xtreme
thanks. I'm just confused because I do not see the advantages of declaring a specialization template.


One advantage appears to be that if the caller specifies the template parameter explicitly then the overload will not get called. Consider the following:

template< typename T > T Sum( T a, T b ){    return a + b;}template<> float Sum( float a, float b ){    return a + b;}float Sum( float a, float b ){    return a + b;}int main( int argc, char* argv[] ){    int   i = Sum( 1, 2 );    float x = Sum( 1.0f, 2.0f );    float y = Sum<float>( 1.0f, 2.0f );    float z = Sum<float>( 1.0f, 2 );}


I compiled this with VS2003 and found that the last two calls will end up in the template specialization, not the overloaded version. Furthermore, if you comment out the specialized template function, the calls will end up in the vanilla template function, not the overload.

So, in the end, I think it's safer to do the template specialization instead of an overload because it encompasses all calling cases.

-John
John
Xtreme
Xtreme
i get the idea. Thanks for enlightening me on specialization and partial templates!
Yes! There are kangaroos in Australia but I haven't seen them...yet
Qw3r7yU10p!
Qw3r7yU10p!
Also, practically, with most compilers, the overload doesn't have to be defined in the header whereas all templates do.

I know its not a big deal but it is a difference.

What about this, gurus?

template<typename T>T Sum(T a, T b) {    return a + b;}float Sum(float, float);//defined in a .cpp somewheretemplate<> float Sum(float a, float b) {    return Sum(a, b);//calls non-templated version?}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.