CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: A const problem

  1. #1
    Join Date
    Oct 2008
    Posts
    45

    Talking 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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Oct 2008
    Posts
    45

    Re: A const problem

    you sir are smart

    nice catch

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  5. #5
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: A const problem

    Quote Originally Posted by TheCPUWizard View Post
    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.

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: A const problem

    Why C++ TMP program must use enum to achieve its purpose?

    Thanks.
    Thanks for your help.

  7. #7
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: A const problem

    Quote Originally Posted by Peter_APIIT View Post
    Why C++ TMP program must use enum to achieve its purpose?

    Thanks.

    Its because the calculations are done at compiletime and not at runtime.

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: A const problem

    Quote Originally Posted by Russco View Post
    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

  9. #9
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: A const problem

    Quote Originally Posted by Russco View Post
    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.

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: A const problem

    Quote Originally Posted by ltcmelo View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured