Non-type template parameters

C++Templates

C++ Problem Overview


I understand that non-type template parameters should be a constant integral expression. Can someone shed light why this is so?

template <std::string temp>
void foo()
{
     // ...
}

> error C2993: 'std::string' : illegal type for non-type template parameter 'temp'.

I understand what a constant integral expression is. What are the reasons for not allowing non-constant types like std::string as in the above snippet ?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

That is not allowed.

However, this is allowed:

template <std::string * temp> //pointer to object
void f();

template <std::string & temp> //reference to object
void g();

See §14.1/6,7,8 in C++ Standard (2003).


Illustration:

template <std::string * temp> //pointer to object
void f()
{
   cout << *temp << endl;
}
 
template <std::string & temp> //reference to object
void g()
{
     cout << temp << endl;
     temp += "...appended some string";
}
 
std::string s; //must not be local as it must have external linkage!
 
int main() {
        s = "can assign values locally";
        f<&s>();
        g<s>();
        cout << s << endl;
        return 0;
}

Output:

can assign values locally
can assign values locally
can assign values locally...appended some string

Solution 3 - C++

You need to be able to mangle template arguments

template <std::string temp>
void f() {
 // ...
}

f<"foo">();
f<"foo">(); // same function?

Now an impl would need to come up with a unique sequence of characters for a std::string or, for that matter, any other arbitrary user defined class, storing a particular value, the meaning of which is not known to the implementation. And in addition, the value of arbitrary class objects can't be calculated at compile time.

It's planned to consider allowing literal class types as template parameter types for post-C++0x (see below), which are initialized by constant expressions. Those could be mangled by having the data members recursively mangled according to their values (for base classes, for example we can apply depth-first, left-to-right traversal). But it's definitely not going to work for arbitrary classes.


As of C++20, we are now allowed to use structural class types as template parameters. In a nutshell, structural classes must have a constexpr constructor, destructor and only structural-type members and base classes (like scalars, arrays thereof or references). They must also only have public and non-mutable base classes and members. These provisions, if the template is instantiated with a constant expression converted to the parameter type, allow the compiler to mangle the argument meaningfully.

Solution 4 - C++

A non-type template argument provided within a template argument list is an expression whose value can be determined at compile time. Such arguments must be:

> constant expressions, addresses of > functions or objects with external > linkage, or addresses of static class > members.

Also, string literals are objects with internal linkage, so you can't use them as template arguments. You cannot use a global pointer, either. Floating-point literals are not allowed, given the obvious possibility of rounding-off errors.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaheshView Question on Stackoverflow
Solution 1 - C++XeoView Answer on Stackoverflow
Solution 2 - C++NawazView Answer on Stackoverflow
Solution 3 - C++Johannes Schaub - litbView Answer on Stackoverflow
Solution 4 - C++SadiqueView Answer on Stackoverflow