Custom template options

I understand that a non-type template parameter must be a constant integral expression. Can someone shed light on 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 mutable types like std::string , as in the above snippet?

+62
c ++ templates
Apr 16 2018-11-11T00:
source share
4 answers

The reason you cannot do this is because mutable expressions cannot be parsed and replaced at compile time. They can change at run time, which will require the generation of a new template at run time, which is not possible because templates are a concept of compilation time.

Here, the standard allows non-piggy type template parameters (14.1 [temp.param] p4):

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

  • integral or enumerated type,
  • a pointer to an object or a pointer to a function,
  • lvalue reference to an object reference or lvalue for a function,
  • member pointer
  • std::nullptr_t .
+89
Apr 16 '11 at 15:33
source share

This 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 the 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 
+57
Apr 16 2018-11-11T00:
source share

You need to be able to manipulate template arguments

 template <std::string temp> void f() { // ... } f<"foo">(); f<"bar">(); // different function!? 

Now, implantation will need to create a unique sequence of characters for std::string or, for that matter, any other arbitrary user class that stores a specific value, the value of which is unknown to the implementation, and besides, the value of arbitrary class objects cannot be computed at compile time.

It is planned to consider the possibility of using literal types as template types for post-C ++ 0x, which are initialized with constant expressions. This can be distorted if the data elements are recursively crippled in accordance with their values โ€‹โ€‹(for base classes, for example, we can use depth traversal, first from left to right). But this will definitely not work for arbitrary classes.

+19
Apr 16 2018-11-11T00:
source share

An argument of a typeless template, presented in the argument list of a template, is an expression whose value can be determined at compile time. Such arguments should be:

constant expressions, addresses of functions or objects with external communication, or addresses of a static class members.

In addition, string literals are internally linked objects, so you cannot use them as template arguments. You also cannot use the global pointer. Floating point literals are not allowed, given the obvious possibility of rounding errors.

+8
Apr 16 2018-11-11T00:
source share



All Articles