Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Template in C++?
2.1.
Class Template
2.2.
Function Template
3.
Types of template specializations in C++
3.1.
Primary Template
3.2.
Partial Specialization
3.3.
Full Specialization
4.
Frequently Asked Questions
4.1.
In C++, how many different forms of specialization are there?
4.2.
What is the purpose of a function template in template specialization in C++?
4.3.
What is the distinction between a template and a function?
4.4.
What is the distinction between partial and template specialization in C++?
5.
Conclusion
Last Updated: Mar 27, 2024

Template specialization in C++

Introduction

C++, or "C with Classes," is a high-level, general-purpose programming language. Danish computer scientist Bjarne Stroustrup developed it to expand the C programming language. It is nearly always written as compiled code. In this blog, we will learn about class template specialization in C++ with the help of examples.

template specialization in C++

What is a Template in C++?

Templates are strong C++ capabilities that allow us to create generic programs. Templates determine the behavior of class and function families. It is frequently necessary to treat unusual types or non-types differently. As a result, you may fully customize templates while using template specialization in C++.

c++

There are two ways we can implement template specialization in C++:

  • Function Templates
  • Class Templates 

Class Template

  • Class templates may be used to construct a single class that works with several data types. 
  • Class templates are helpful since they make our code shorter and easier to manage.
  • A class template begins with the keyword "template," then template parameter(s) inside <>, then the class declaration. 
  • T is a placeholder for the data type used in the template parameter, and class is a keyword.
  • Class templates are partially specialized. 

Syntax:

template <class T>
class className {
  private:
    T var;
    ... .. ...
  public:
    T functionName(T arg);
    ... .. ...
};
You can also try this code with Online C++ Compiler
Run Code

Example:

#include <iostream>
using namespace std;
template<class t1>
class numbers
{
    t1 x;
    public:
        void getdata()
        {
            cin>>x;
        }
        void display()
        {
            cout<<"Your entered value is:"<<endl;
            cout<<"x="<<x<<endl;
        }
};
int main()
{
    numbers<int> s1;
    numbers<float> s2;
    cout <<"Enter an Integer value:"<<endl;
    s1.getdata();
    s1.display();
    cout <<"Enter Float value"<<endl;
    s2.getdata();
    s2.display();
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Output

Dry run:

Class Template

Fibonacci Series in C++

Function Template

A function template begins with the keyword template, then template parameter(s) inside <>, and the function declaration.

Syntax:

template <class T>
T Name(T p1, T p2, ...) {
}
You can also try this code with Online C++ Compiler
Run Code

Here p is the parameter.

Example:

#include <iostream>
using namespace std;
template<class t1,class t2>
void product(t1 x,t2 y) // defining template function
{
    cout<<"Product="<<x*y<<endl;
}
int main()
{
    int p,q;
    float s,r;
    cout<<"Enter two integer data: ";
    cin>>p>>q;
    product(p,q);
    cout<<"Enter two float data: ";
    cin>>s>>r;
    product(s,r); 
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Output

Dry run:

Step 1:

Function Template step 2

Step 2:

Function Template step 2

Try and compile with online c++ compiler.

Types of template specializations in C++

There are three essential segments to understand under "template specialization in C++": 

Primary Template

Before declaring the partially or entirely specialized templates, the primary template must be defined.

template <typename T, int1, int2>
class Number; 
You can also try this code with Online C++ Compiler
Run Code

Partial Specialization

Only class templates allow for partial specialization. A partial specialization contains template parameters and explicitly stated template arguments.

template <typename T, int1, int2>

class Number<T, 5, 8>{}; 
You can also try this code with Online C++ Compiler
Run Code

Full Specialization

The term "full" denotes that all template parameters have been given and the template parameter list is empty:

template <>
class Number<int, 5, 8>{}; 
You can also try this code with Online C++ Compiler
Run Code

Frequently Asked Questions

In C++, how many different forms of specialization are there?

Template specialization in C++ is classified into two categories. There are two types of specialization: complete specialization and partial specialization.

What is the purpose of a function template in template specialization in C++?

For constructing functions that may be utilized with various data types, we use C++ function templates. A function template is used to run the same function on different forms of data. A different method is to use function overloading.

What is the distinction between a template and a function?

In a particular program, a function definition can only be provided once (unless the function is marked inline ). The linker will choose one copy of each actual instantiation of a function template declared in multiple translation units.

What is the distinction between partial and template specialization in C++?

A subset of class template specialization is partial template specialization. It is most commonly used in connection with the C++ programming language. It allows the programmer to specialize just some class template parameters instead of using explicit complete specialization, which provides all template arguments.

Conclusion

In this blog, we will learn about class template specialization in C++ with the help of examples. If you think this blog has helped you enhance your knowledge of the above question or want to learn more, check out our articles. Visit our website to read more such blogs. 

For placement preparations, you must look at the problemsinterview experiences, and interview bundles. Enroll in our courses and refer to the mock tests and problems available; look at the Problem Sheets interview experiences, and interview bundle for placement preparations. You can also book an interview session with us.  

Consider our paid courses, however, to give your career a competitive advantage!

Happy Coding!

Live masterclass