Possible Duplicate:
What does template <unsigned int N> mean?
Hi ! Are non-type template parameters and constants same ? Do the following code work because template parameter cannot be modified ? If can be modified, the compiler should have thrown error while declaring array “a[T]”. Is my understanding correct ?
template < int T >
void foo() {
int a[T] ;
}
int main( int argc, const char* argv[] ) {
foo <3> () ;
system("pause") ;
return 0 ;
}
Yeah, kind of. Thing is every time you instantiate a template the compiler will generate specific code for that specific type parametrization. So for instance, in your example if you have
foo<3>andfoo<5>the compiler will generate code for two separate functions one whereT=3and one whereT=5So yeah, it works because
Tcan’t change, the mechanism why it works is slightly more complex though…