   C++ constants
  HiI'd like to let this code do most of my talking.template <int N>class X {};int func() { return 10; }const int a = 10;const int b = func();int main(){    X<a> A;    X<b> B;    // Error}In the above code, constant a can be passed into template arguments while b cannot. I don't know what exactly they call, but a acts like an enum constant and b acts like just a global variable (except it cannot be modified).What's the rule behind that?Your information would be much appreciated.Thanks   
  I suppose that the reason is that templates are evaluated at compile time, while functions are avaluated at run time (aven while a smart compiler could notice that this function will ever have the same value and thus acts like a constant)...So, basically, when the compiler is asked to instantiate the template it doesn't recongize func() as a valid value. What does the error say? Are optimizations on or off? What if you put func() inline (shouldn't matter really)?Which compiler are you using?   ;  Sadly, the compiler cannot assume that func() returns a constant value. This means, that for the purposes of creating templates, any function is considered a runtime value.As a general rule, templates can only be created with either literals or const values initated with a literal.Happily, however, C++0x gives you a remedy for this: the constexpr modifier.//Instead of int func() { return 10; }//We could doconstexpr int func() { return 10; }//And the template stuff would work happilyUnfortuneatly, this won't work in most compilers avaliable today; you might have to wait for a few years.   ;  Thanks for your reply.Quote:Original post by cignox1What does the error say?It saysQuote:error C2970: 'X' : template parameter 'N' : 'b' : an expression involving objects with internal linkage cannot be used as a non-type argumentQuote:Are optimizations on or off? What if you put func() inline (shouldn't matter really)?They don't affect anything.Quote:Which compiler are you using?I'm using Visual C++ 2005I think I didn't state my question clearly. (I should go and wash my face now :D)What I was trying to do istemplate <float f>class X {};const float PI = 3.141593f;const float a = sinf(PI / 4);int main(){    X<a> A;    // Error}Any solution?Thanks;  Basically I don't believe you can use the result of a sinf call for a template.You could calculate the result yourself, or perhaps find/write a metaprogramming library that is able to calculate sin at compile-time (probably not a trivial task).--------------Quote:I don't like the name std::vector. I'd be happy if it was std::array.They are rather different, though. And there is also a boost::array.   ;  template<float x> is illegal anyways, according to the standard.   ;  Thanks all. I didn't read error messages carefully. Very sorry for useless question ;-(;  The effect can be got if using something likestruct SinOfPiOver4 {static const float value;};const float SinOfPiOver4::value = 0.70710678f;template<typename x>class X {// do something with x::value;};X<SinOfPiOver4>  x;if it is worth the inconvenience.   ;Quote:Original post by haegarrThe effect can be got if using something like*** Source Snippet Removed ***if it is worth the inconvenience.Thanks very much. Perfect solution.   