A non-type template parameter (NTTP) is a template parameter that expects a compile-time constant value rather than a type. When instantiated, the compiler substitutes the parameter with the provided constant expression, making the value an integral part of the generated type or function signature.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Syntax
Non-type template parameters are declared by specifying a concrete type (orauto) in the template parameter list, followed by an identifier.
Permitted Types
The types allowed for non-type template parameters are strictly regulated by the C++ standard and have expanded across standard revisions. Prior to C++17, non-type template parameters were restricted to:- Integral types (
int,char,long,std::size_t,bool, etc.) - Enumeration types (
enumandenum class) - Pointers or references to objects, functions, or class members (the referenced entity was required to have static storage duration and external or internal linkage)
std::nullptr_t
- Floating-point types (
float,double) - Literal class types. To qualify as a structural type, the class must have
constexprconstructors, no virtual base classes, no virtual functions, all base classes and non-static data members must be public and structural, and neither the class nor its base classes may have anymutablenon-static data members.
auto Non-Type Template Parameters (C++17)
Since C++17, the auto keyword can be used to instruct the compiler to deduce the type of the non-type template parameter directly from the provided argument.
Technical Constraints
- Compile-Time Evaluation: The argument passed to a non-type template parameter must be a constant expression (
constexpr). It cannot be a variable whose value is determined at runtime. - Value Category and Mutability: The properties of an NTTP within the template definition depend on its type:
- Non-reference types (e.g.,
int,enum, pointers): The parameter acts as a prvalue (pure rvalue). It is strictly read-only, and its address cannot be taken. - Reference types (e.g.,
int&,MyClass&): The parameter acts as an lvalue. You can take its address (which yields the address of the bound object), and you can modify the referenced object provided the reference is notconst.
- Non-reference types (e.g.,
- Type Identity: Instantiations with different non-type template arguments result in distinct, incompatible types. For example,
StaticArray<int, 5>andStaticArray<int, 6>are completely separate types in the C++ type system. - String Literals (Pre-C++20): Passing string literals directly (e.g.,
Template<"string">) is prohibited before C++20 because string literals have no linkage and their addresses are not guaranteed to be unique across translation units. C++20 allows string-like NTTPs by passing them through structural wrapper classes that store the string characters as an array.
Master C++ with Deep Grasping Methodology!Learn More





