Non-type template parameters

The reason you can’t do this is because non-constant expressions can’t be parsed and substituted during compile-time. They could change during runtime, which would require the generation of a new template during runtime, which isn’t possible because templates are a compile-time concept.

Here’s what the standard allows for non-type template parameters (14.1 [temp.param] p4):

A non-type template-parameter shall have one of the following (optionally cv-qualified) types:

  • integral or enumeration type,
  • pointer to object or pointer to function,
  • lvalue reference to object or lvalue reference to function,
  • pointer to member,
  • std::nullptr_t.

Leave a Comment