|
-
November 13th, 2008, 03:59 PM
#1
A const problem
is there a power function that is concidered const? like a^2?
my problem is:
const int a=2;
char b[a];
double b[pow(2,a)];
this is not possible since pow is not const, now 2*a will work (if that is what you want) but power operation doesn't since there is no operator that is concidered constant by the compiler (I guess), is there a solution for this other than:
const int a=2;
const int b=4;
double a[b];
my real code is here:
Code:
template <int branchOutNum=2,int k=1,int n=3> // note branchOutNum=2^k is a must
class viterbiState
{
viterbiState *states[branchOutNum];
char input[n];
char estimate[k];
viterbiState(){}
};
so I need both k and 2^k (banchOut) and it is a must that branchOut=2^k, so I just would need the user to enter them as so, otherwise I'm in trouble, and I think thats not good programming.
so any ideas to solve this one?
thanks
-
November 13th, 2008, 04:06 PM
#2
Re: A const problem
For powers of 2, you're in luck---you can use the bit-shift operator. So for an array of 32 elements, you'd do:
Code:
char myarray[1<<5];//1<<5 is 1 * 2^5.
-
November 13th, 2008, 04:16 PM
#3
Re: A const problem
you sir are smart 
nice catch
-
November 13th, 2008, 04:16 PM
#4
Re: A const problem
Also read the book "Modern C++ Design" by Andrei Alexandrescu.
Simply AMAZING what you can accomplish....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 13th, 2008, 04:45 PM
#5
Re: A const problem
 Originally Posted by TheCPUWizard
Also read the book "Modern C++ Design" by Andrei Alexandrescu.
Simply AMAZING what you can accomplish....
Yeah you're not kidding. That guy is a genius.
You will learn much from that book, stuff like how to work out powers at compile time. Heres how its done for powers of 2. Im sure you can expand it to other powers too with little effort.
Code:
template < int num >
class Pow
{
public:
enum { _result = 2 * Pow< num - 1 >::_result };
};
template <>
class Pow < 0 >
{
public:
enum { _result = 1 };
};
compiletime recursion is a wonderful thing sometimes.
-
November 14th, 2008, 05:09 AM
#6
Re: A const problem
Why C++ TMP program must use enum to achieve its purpose?
Thanks.
Thanks for your help.
-
November 14th, 2008, 05:17 AM
#7
Re: A const problem
 Originally Posted by Peter_APIIT
Why C++ TMP program must use enum to achieve its purpose?
Thanks.
Its because the calculations are done at compiletime and not at runtime.
-
November 14th, 2008, 08:18 AM
#8
Re: A const problem
 Originally Posted by Russco
Its because the calculations are done at compiletime and not at runtime.
That is NOT the "Reason", and infact (in many cases) the word "Must" is inappropriate.
When C++ was in the draft stage, you could NOT initialize const integers inside the header. Later it was decided that this was a useful feature and added to the language specification. By this time there were amny compilers on the market, but they (since they were developed earlier) did not support this feature.
One simple test of standards compliance:
Code:
class TestISO
{
private:
static const int Size = 100;
int Data[Size];
};
If this code does NOT compile, then your compiler is not a compliant C++ compiler.
Andre's book was published at a time where this was not commonly supported, and so many cases where an int can not be used, simply would not have worked at the time of publication.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 14th, 2008, 08:28 AM
#9
Re: A const problem
Yeah you can use static consts, enums, typedefs. Basically anything that can be worked out at compiletime. Whatever the point is it has to be const and loops done with recursion as iteration requires something non-const as a loop counter.
ok there are other options but i always found enums fairly intuitive and acceptable on all compilers I've played with.
-
November 14th, 2008, 09:19 AM
#10
Re: A const problem
 Originally Posted by Russco
Yeah you're not kidding. That guy is a genius.
You will learn much from that book, stuff like how to work out powers at compile time. Heres how its done for powers of 2. Im sure you can expand it to other powers too with little effort.
Code:
template < int num >
class Pow
{
public:
enum { _result = 2 * Pow< num - 1 >::_result };
};
template <>
class Pow < 0 >
{
public:
enum { _result = 1 };
};
compiletime recursion is a wonderful thing sometimes.
In the case of this particular example, I'm not sure whether he is the "original inventor". It's a variation of the well-known compile-time factorial, which I think was published before his book. In fact, there's an interesting history regarding template metaprogramming.
-
November 14th, 2008, 09:27 AM
#11
Re: A const problem
 Originally Posted by ltcmelo
In the case of this particular example, I'm not sure whether he is the "original inventor"..
MOST things have tracable roots that go far back in time before the publication that brought them into widespread knowledge and usage.
Like: "Design Patterns : Elements of Reusable Software", this book is considered by many people to be the seminal reference.
Many job intereciews for (non-entry level) C++ positions will have at least some material from this book.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|