One of the great features of modern C++ is templates. A template is a simple and very powerful statement in C++ that defines the operations of a class or function. Templates are parameterized features of C++. In this article, we will explain non-type parameters for the template in C++ which can be used by a professional C++ IDE that supports C++14, C++17, and over compilers.
First of all, let’s try to explain what templates are in C++.
Table of Contents
What is a template in C++?
A template is a very powerful statement in C++ that simply defines the operations of a class, a function, an alias, or a variable and let the user may apply the same template on different types in those template operations. Templates are like macros in C++ except the compiler checks the types used before the template is expanded. In the compilation mechanism of a template in C++, the source code contains only a template for a function or class, but when it is compiled, the same template can be used on multiple data types.
Here is the syntax of a template:
1 2 3 |
template < parameters > declaration |
In this article we will explain how to use non-type template parameters in C++.
What are non-type parameters for templates in C++?
A template has at least one template parameter.
Parameters of a template can be:
- a type template parameter,
- a non-type template parameter,
- a template template parameter (a template passed as a parameter to the template).
A non-type template parameter (aka non-type template argument) is provided within a template parameters list and it is an expression whose value can be determined at compile time. A non-type template parameter can be a constant expression, address of function or object with external linkage, or address of static class member. Generally, non-type template parameters are used to specify the sizes of class members or to initialize a class.
A non-type parameter can be one of the following types,
- An integral type
- An enumeration type
lvalue
reference type- A pointer type or reference to a class object
- A pointer or reference to a function
- A pointer or reference to a class member function
std::nullptr_t
- A floating point type (since C++20)
Is there an example of non-type parameters for templates in C++?
Here is a simple a non-type template parameter example,
1 2 3 4 5 |
template <auto a> class temp { }; |
here is a integral type for a non-type parameter that used to resize a std::array
in C++ so that it has n
elements.
1 2 3 4 5 6 |
template <int n> class mytemp { std::array<float, n> ar; }; |
and we can use this template as below,
1 2 3 |
mytemp<100> t; |
Is there a full example of non-type parameters for templates in C++?
Here a is a full C++ example of a non-type template parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <array> // Non-type template parameters template <int n> class mytemp { std::array<float, n> ar; }; int main() { mytemp<100> t; } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition