static constant template member problems
-
I 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... -
I 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...I
andi
are different variables entirely. And making them static probably won't reduce memory usage. The values will all be known at compile time, so the compiler will be able to directly insert the values (unless you take the address ofiminus1
or one of the other members). --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Ford, what's this fish doing in my ear? -
I
andi
are different variables entirely. And making them static probably won't reduce memory usage. The values will all be known at compile time, so the compiler will be able to directly insert the values (unless you take the address ofiminus1
or one of the other members). --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Ford, what's this fish doing in my ear?Hi 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
-
Hi 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
Unfortunately, I'm not seeing the problem you are. Here's the code I tested on VC 7.1:
#include <iostream>
#include <tchar.h>
using namespace std;template <class T, int I> class X
{
public:
X() {}
static const icopy = I;
static const iminus1 = I-1;
static const itimes8 = I<<3;int array\[I\];
};
int _tmain(int argc, _TCHAR* argv[])
{
X<int, sizeof(int)> x1;cout << "x1.icopy = " << x1.icopy << "\\nx1.iminus1 = " << x1.iminus1 << "\\nx1.itimes8 = " << x1.itimes8 << '\\n'; return 0;
}
Output:
x1.icopy = 4
x1.iminus1 = 3
x1.itimes8 = 32--Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Pinky, are you pondering what I'm pondering? I think so Brain, but how will we fit the hamster inside the accordion?
-
Unfortunately, I'm not seeing the problem you are. Here's the code I tested on VC 7.1:
#include <iostream>
#include <tchar.h>
using namespace std;template <class T, int I> class X
{
public:
X() {}
static const icopy = I;
static const iminus1 = I-1;
static const itimes8 = I<<3;int array\[I\];
};
int _tmain(int argc, _TCHAR* argv[])
{
X<int, sizeof(int)> x1;cout << "x1.icopy = " << x1.icopy << "\\nx1.iminus1 = " << x1.iminus1 << "\\nx1.itimes8 = " << x1.itimes8 << '\\n'; return 0;
}
Output:
x1.icopy = 4
x1.iminus1 = 3
x1.itimes8 = 32--Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Pinky, are you pondering what I'm pondering? I think so Brain, but how will we fit the hamster inside the accordion?
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
-
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
Great, I'm glad it worked out for you. :) Funny thing about the debugger, you could report that to MS so they fix it for Whidbey. ;) --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Pinky, are you pondering what I'm pondering? I think so Brain, but if we shaved our heads, we'd look like weasels! Actual sign at the laundromat I go to: "No tinting or dying." "Kosh reminded me of some of the prima-donna programmers I've worked with. Knew everything but when you asked them a question; never gave you a straight answer." -- Michael P. Butler in the Lounge