Does template specialization have any use?

Hi Guys,
I am studying templates in C++, and just run into this question:"Does template specialization have any use?", because I think if no general type argument, what does these templates use for?

Thanks in advance:)
Last edited on
numeric_limits is an example for such a specialization:

http://www.cplusplus.com/reference/limits/numeric_limits/?kw=numeric_limits
One form of template specialization:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program
#include <iostream>
#include <string>

// general
template <class T>
void println(const T val)
{
   std::cout << val << "\n";
}
// specialised for bool
template <>
void println(const bool val)
{
   std::cout << std::boolalpha << val << "\n";
}


int main()
{
  println(5);
  println(true);
}
Template metaprogramming requires specialization to make decisions, thus specialization is required to write recursive metafunctions, otherwise they would never terminate (at least not without the compiler rejecting the program or crashing).
Last edited on
@Thomas1965

My only objection with your example is that it compiles and runs correctly whether or not you have the "template specialization" syntax in there or not -- which, if it's the only example, reinforces OP's original thought. (I'm not too knowledgeable on template specialization myself so I can't really give my own proper example.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Example program
#include <iostream>
#include <string>

// general
template <class T>
void println(const T val)
{
   std::cout << val << "\n";
}
// specialised overloaded (thanks Cubbi) for bool
void println(const bool val)
{
   std::cout << std::boolalpha << val << "\n";
}


int main()
{
  println(5);
  println(true);
}
Last edited on
// specialised for bool
void println(const bool val)

that's an overload for bool. Functions overload and specialize. Classes only specialize.
@Thomas1965
I got it from your examples. Template specialization talks about templates' specialization. First
of all, templates are used for defining a bunch of similar functions/classes, but if there're special cases like needing different behaviors under certain data types, we need template specialization. Thx all guys, really helpful community here.
As a general principle, strongly favour overloading over specialisation for function templates.
See: http://www.gotw.ca/publications/mill17.htm
@Cubbi,

in "The C++ Programming Language" 3rd edition p.344 B.Stroustrups gives the following example for template specialisation:
1
2
3
4
5
6
7
8
9
10
11
template <class T>
bool less(T a, T b)
{
  return a < b;
}

template<>
bool less<const char *>(const char* a, const char* b)
{
  return strcmp (a, b) < 0;
}


As for classes the template<> prefix saysthat this is a specialization that can be specified without a template parameter.


So I guess my example should be
1
2
3
4
5
template<>
void println<bool>( bool val)
{
   std::cout << std::boolalpha << val << "\n";
}
So I guess my example should be

yes, but as JLBorges correctly points out, for function templates overloading is better in every way. Specializations are for class templates (and variable templates).
Speaking of less, here's an example of a full specialization of a class template in standard C++: http://en.cppreference.com/w/cpp/utility/functional/less_void

Here are two more: http://en.cppreference.com/w/cpp/locale/ctype_char http://en.cppreference.com/w/cpp/container/vector_bool

And there's a partial specialization of a class template: unique_ptr<T[]> (2'nd definition in http://en.cppreference.com/w/cpp/memory/unique_ptr )
Topic archived. No new replies allowed.