Non-type template parameters

2019-01-01 06:59发布

I understand that the non-type template parameter should be a constant integral expression. Can someone shed light why is it 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++ templates
4条回答
余生请多指教
2楼-- · 2019-01-01 07:16

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.

查看更多
只若初见
3楼-- · 2019-01-01 07:20

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.
查看更多
浅入江南
4楼-- · 2019-01-01 07:36

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
查看更多
萌妹纸的霸气范
5楼-- · 2019-01-01 07:38

You need to be able to mangle template arguments

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

f<"foo">();
f<"bar">(); // different 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, 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.

查看更多
登录 后发表回答