Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of template<> with empty angle brackets in C++?

Tags:

c++

templates

template<> class A{ //some class data }; 

I have seen this kind of code many times. what is the use of template<> in the above code? And what are the cases where we need mandate the use of it?

like image 763
Vijay Avatar asked Jun 09 '11 06:06

Vijay


People also ask

What does template <> mean in CPP?

A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

What is function template C?

Function templates. Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

Can we use template in struct?

You can template a struct as well as a class. However you can't template a typedef. So template<typename T> struct array {...}; works, but template<typename T> typedef struct {...}

Do we have template in C?

The main type of templates that can be implemented in C are static templates. Static templates are created at compile time and do not perform runtime checks on sizes, because they shift that responsibility to the compiler.


2 Answers

template<> tells the compiler that a template specialization follows, specifically a full specialization. Normally, class A would have to look something like this:

template<class T> class A{   // general implementation };  template<> class A<int>{   // special implementation for ints }; 

Now, whenever A<int> is used, the specialized version is used. You can also use it to specialize functions:

template<class T> void foo(T t){   // general }  template<> void foo<int>(int i){   // for ints }  // doesn't actually need the <int> // as the specialization can be deduced from the parameter type template<> void foo(int i){   // also valid } 

Normally though, you shouldn't specialize functions, as simple overloads are generally considered superior:

void foo(int i){   // better } 

And now, to make it overkill, the following is a partial specialization:

template<class T1, class T2> class B{ };  template<class T1> class B<T1, int>{ }; 

Works the same way as a full specialization, just that the specialized version is used whenever the second template parameter is an int (e.g., B<bool,int>, B<YourType,int>, etc).

like image 132
Xeo Avatar answered Sep 18 '22 16:09

Xeo


template<> introduces a total specialization of a template. Your example by itself isn't actually valid; you need a more detailed scenario before it becomes useful:

template <typename T> class A {     // body for the general case };  template <> class A<bool> {     // body that only applies for T = bool };  int main() {     // ...     A<int>  ai; // uses the first class definition     A<bool> ab; // uses the second class definition     // ... } 

It looks strange because it's a special case of a more powerful feature, which is called "partial specialization."

like image 20
John Calsbeek Avatar answered Sep 22 '22 16:09

John Calsbeek