//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);
🔒 Locked
templates
Advertisement
Original Post
In C++, do the following templates have the same meaning? 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?
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.
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 ..."
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?
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!
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
#4 isn't a template
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);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.
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.
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.
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.
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
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?
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.
Advertisement