Why does the template value parameter have to be constant if code generation is performed at runtime?
template<int a> void foo () { cout << a << endl; } int main() { int b = 10; foo<b>(); return 0; } Why does the template value parameter have to be constant if code generation is performed at runtime?
template<int a> void foo () { cout << a << endl; } int main() { int b = 10; foo<b>(); return 0; } In general, it is not clear what you want to write.
If you need a template printing function, then you do it wrong, you need this:
template<typename T> void foo (const T& var) { std::cout << var << std::endl; } int main() { int b = 10; foo(b); return 0; } When compiling this code, the template function foo will be expanded for type int.
On the syntax of the templates a lot of information on the Internet.
http://cppstudio.com/post/5188/
https://ru.wikipedia.org/wiki/%D0%A8%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD%D1%8B_C%2B%2B
const int b = 10 everything works - VitalySource: https://ru.stackoverflow.com/questions/826481/
All Articles