Partial Template Specialization in C++
Last Updated :
04 Dec, 2023
In C++, template specialization enables us to define specialized versions of templates for some specific argument patterns. It is of two types:
- Full Template Specialization
- Partial Template Specialization
In this article, we will discuss the partial template specialization in C++ and how it is different from the full template specialization
Partial Template Specialization in C++
In C++, partial template specialization allows us to define a specialized version of the template for some of the template arguments in contrast to full template specialization which requires all the arguments to be defined.
Partial template specialization can be defined for all template types, i.e., class template, function template, and variable template.
Syntax of Partial Template Specialization
The syntax for partial template specialization depends on our requirement, template type, and number of templates. In General,
1. For Class Templates
Consider the primary class template:
template <class T, class X>
class Geek {
// General implementation for the primary template
};
If we want a specialization of the class Geek only for argument X as an integer, we use template specialization like this:
template < class T>
class Geek<T, int> {
// Specialized implementation for the primary template
}
2. For Function Templates
Let the primary function template be:
template <typename A, typename B>
void function(A var, B str) {
// General implementation for the primary template
}
To specialize the above function template for A as a pointer, we can use the following syntax:
template <typename A, typename B>
void function (A* var, B str) {
// Specialized implementation for the primary template
}
Examples of Partial Template Specialization
Example 1: C++ Program to illustrate Partial Specialization of Class Template
C++
// C++ Program to illustrate Class Template Partial
// Specialization
#include <iostream>
using namespace std;
// Primary template
template <typename T, typename X>
class Geek {
public:
void Print() { cout << "Primary Template" << endl; }
};
// Partial specialization for X as int
template <typename T> class
Geek<T, int> {
public:
void Print()
{
cout << "Partial Specialization for int" << endl;
}
};
// driver code
int main()
{
Geek<bool, double> obj1;
Geek<bool, int> obj2;
obj1.Print();
obj2.Print();
return 0;
}
OutputPrimary Template
Partial Specialization for int
Example 2: C++ Program to illustrate Partial Specialization of Function Template
C++
// C++ Program to illustrate Function Template Partial
// Specialization
#include <iostream>
using namespace std;
// Primary template
template <typename A, typename B>
void foo(A var, B str)
{
cout << "Primary Template" << endl;
}
// Partial specialization for A as pointer
template <typename A, typename B>
void foo(A* var, B str)
{
cout << "Partial Specialization Template" << endl;
}
// driver code
int main()
{
int var = 10;
int* ptr = &var;
foo(var, "Geek");
foo(ptr, 24);
return 0;
}
OutputPrimary Template
Partial Specialization Template
Similar Reads
Template Specialization in C++
Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type. What if we want a different code for a p
5 min read
Temporary Materialization in C++ 17
C++ is a popular programming language that is widely used in the development of system software, application software, game engines, and more. One of the most significant improvements in C++17 is the introduction of temporary materialization, a feature that helps reduce the complexity of code and im
3 min read
std::partial_sort in C++
std::sort is used for sorting the elements present within a container. One of the variants of this is std::partial_sort , which is used for sorting not the entire range, but only a sub-part of it. It rearranges the elements in the range [first, last), in such a way that the elements before middle ar
7 min read
std is_object Template in C++
The std::is_object template of C++ STL is used to check whether the given type is object or not. It returns a boolean value showing the same. Syntax: template <class T > struct is_object; Parameter: This template accepts a single parameter T (Trait class) to check whether T is a object type or
2 min read
is_final template in C++14
The std::is_final template of C++ STL is used to check whether the type is a final or not. Syntax: template < class T > struct is_final; Parameter: This template contains single parameter T (Trait class) to check whether T is a final type. Return Value: This template returns a boolean value as
2 min read
is_polymorphic template in C++
The std::is_polymorphic template of C++ STL is used to checks whether the type is a polymorphic class type or not. It returns a boolean value showing the same. Syntax: template < class T > struct is_polymorphic; Parameter: This template contains single parameter T (Trait class) to check whethe
2 min read
is_fundamental Template in C++
The is_fundamental template of C++ STL is used to check whether the type is a fundamental type or not. It returns a boolean value showing the same. Syntax: template <class T> struct is_fundamental; Parameter: This template accepts a single parameter T (Trait class) to check whether T is a fund
2 min read
std::partition in C++ STL
C++ has a class in its STL algorithms library which allows us easy partition algorithms using certain inbuilt functions. Partition refers to act of dividing elements of containers depending upon a given condition. Partition operations :1. partition(beg, end, condition) :- This function is used to pa
5 min read
C++ Standard Template Library (STL)
The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
Class Template Argument Deduction in C++17
In this article, we will learn about Class Template Argument Deduction(CTAD) in C++17 and with examples. CTAD is a feature in C++17 that allows the template arguments to be deduced from constructor arguments. In simple words, we can say that instead of explicitly specifying the template arguments th
4 min read