Template non-type arguments in C++ Last Updated : 29 Dec, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisite: Templates in C++ Generally, a C++ template, with a single argument looks like this: template<typename template_name> But it has been seen that a template can have multiple arguments. The syntax for the same would be: template<class T1, class T2, class T3, ........., class Tn> where, n is the number of arguments. It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types. Example 1: template <class T, int size> class Array { private: // Automatic array initialization T Arr[size] ..... ..... }; Explanation: In the above example, the template supplies the size of the array as an argument. This implies that the size of the array is known to the compiler at the compile time itself. The arguments must be specified whenever a template class is created. Example 2: // Array of 10 integers Array<int, 10> a1 // Array of 5 double type numbers Array<double, 5> a2 // String of size 9 Array<char, 10> a3 where size is given as an argument to the template class. Following are the arguments that are allowed: Constant Expressions Addresses of function or objects with external linkage Addresses of static class members. Below is the program to illustrate the Non-type templates: C++ // C++ program to implement bubble sort // by using Non-type as function parameters #include <iostream> using namespace std; // Function to swap two numbers template <class T> void swap_(T* x, T* y) { T temp = *x; *x = *y; *y = temp; } // Function to implement the Bubble Sort template <class T, int size> void bubble_sort(T arr[]) { for (int i = 0; i < size - 1; i++) { // Last i elements are already // in place for (int j = 0; j < size - i - 1; j++) { if (arr[j] > arr[j + 1]) { // Swap operation swap_(&arr[j], &arr[j + 1]); } } } } // Function to print an array template <class T, int size> void printArray(T arr[]) { int i; for (i = 0; i < size - 1; i++) { cout << arr[i] << ", "; } cout << arr[size - 1] << endl; } // Driver Code int main() { // Given array arr[] float arr[] = { 1.1, 1.2, 0.3, 4.55, 1.56, 0.6 }; const int size_arr = sizeof(arr) / sizeof(arr[0]); // Size of the array passed as // an argument to the function bubble_sort<float, size_arr>(arr); cout << "Sorted Array is: "; printArray<float, size_arr>(arr); return 0; } Output: Sorted Array is: 0.3, 0.6, 1.1, 1.2, 1.56, 4.55 Comment More infoAdvertise with us Next Article Template non-type arguments in C++ A abhishekdutt32 Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2020 C++-Function Overloading and Default Arguments C++-Templates Templates +2 More Practice Tags : CPP Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read 30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is 15 min read Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are 10 min read Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio 10 min read Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Balance Factor = left subtree height - right subtree heightFor a Balanced Tree(for every node): -1 ⤠Balance Factor ⤠1Example of an 4 min read What is a Neural Network? Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental 12 min read Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so 9 min read C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i 15+ min read Like