Mike, Sometimes, it just helps when someone forces you to look at your code again eh!!? Whilst we've been discussing this, I converted the template class so that it simply used standard members and discovered that there was a general error in some of my logic that was making it appear that these static constants were getting initialised to 1. Of course, however, they weren't - as you've demonstrated and I've just verified. What didn't help - and I'm not going mad on this one - is that the VC++ debugger picks up these values at run-time as 1 (perhaps understandably, since their values are dependent on the template args), regardless of what value they actually have, therefore I had just assumed that the compiler was stupid, and not me. I should have remembered that the console project is my friend when it comes to debugging values. Thanks to your help, I have now converted it back to static constants and have received a fifth of a second speed increase over the course of 250,000 operations that will use this class - not much, but enough for me to be happy :-D! Thanks very much for your help, Zoltan I'm not as evil as my name suggests
Andras Zoltan
Posts
-
static constant template member problems -
what is LValue error?.I can only assume you mean when the compiler is expecting an l-value on the left-hand side of an assignment:
i=10;
is fine so long as i was not defined as a constant. More obviously:10=3;
will give you an l-value error because 10 is not an l-value and therefore cannot be assigned to anything else. 10 is 10, and cannot be anything else!:-D I hope this helps, Zoltan -
static constant template member problemsHi Mike, Thank you for your reply - Michael Dunn wrote: I and i are different variables entirely. And making them static probably won't reduce memory usage I was being stupid in my code example - I am trying to use the template argument I to assign my static constants in order to reduce computational overhead in any functions that need to use them. I accidentally put i instead because I was half asleep when I posted the question! :zzz: In theory, therefore, if I use the template argument, which will be a compile-time constant of, say, 4, to calculate the other constants, how can icopy be getting set to 1 and not 4? Thanks
-
static constant template member problemsI have a template class inside which I wish to embed some static constants to speed up some time-critical code. Consider this:
template <class T, int I> class X { private: static const icopy = i; static const iminus1 = i-1; static const itimes8 = i<<3; int array[I]; public: (..) };
I is assigned by the code using the template to the size of T, which is fine when the compiler uses it to create the array with num elements equal to I. However, all of the static constants appear to be getting initialised to 1, even when I is explicitly set to 4 or 8. I wish to make these constants static in order to reduce memory overhead, therefore a solution to this problem is kinda essential to me? Am I mis-using the language? Or is it an example of VS.Net 2003's C++ compiler's non-conformance? :confused: Please help! Thanks in advance...