Tuesday, 5 July 2016

Whether non-type template parameter can be assigned with some value?

 
Guess the output?

#include<iostream>

template<int a>
void PostInArrayNotFound(int b)
{
a=10;
std::cout<<a<<std::endl;
std::cout<<b<<std::endl;
}

int main()
{
PostInArrayNotFound<2>(25);
return 0;
}

Output:
Compilation error.

Explanation:

Non-type template parameter is an read value not an lvalue. For lvalue you cannot assign anything.

a=10; is similar to 2=10; after compilation process.


During compilation time, compiler will replace the a as 2.