1

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 ; } 
4
  • @ybungalobill Thanks for editing the code part.The way I did was - started with <pre><code> tag and ended with <pre></code>. And for each end of line, I have given 1 space. While in preview, it shows correctly. But after posting, it is changing? It would be helpful for further posts if you can say me the way you edited ? Commented Dec 27, 2010 at 21:08
  • @marcog The link was very helpful. Thanks. Commented Dec 27, 2010 at 21:08
  • @marcog: I agree. Especially Johannes' answer is very exhaustive. Commented Dec 27, 2010 at 21:08
  • 1
    You can see it here. You begin each line of the code with 4 spaces, or just use the code button on the toolbar: {} Commented Dec 27, 2010 at 21:11

2 Answers 2

6

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> and foo<5> the compiler will generate code for two separate functions one where T=3 and one where T=5

So yeah, it works because T can't change, the mechanism why it works is slightly more complex though...

Sign up to request clarification or add additional context in comments.

Comments

2

Yes, non-template parameters have to be constant expressions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.