Why is it illegal to use a local variable as a non-type argument?
For example, in the next code local_var
cannot be argument to X
.
The "value" of a template needs to be present at compile time.
template struct X {};
Even if you do not bind a reference or pass a pointer here, the compiler must know the value of the passed elements at compile time.
Replacing int &x
with int x
is on purpose here. Stuff about int& is answered correctly. I just wanted to point that it applies to all non-typed template arguments.
template
is an address...
X x; // will not work, local_var does not exist at compile time
X<1> x; // works since 1 is known
I just wanted to (in addition to Andy's answer) prevent any conclusions that would suggest to use a value type instead of a reference.