computation at compile time i.e. compile time functions usingtemplates

Discussion in 'C++' started by Carter, Mar 4, 2009.

  1. Carter

    Carter Guest

    Hi,

    I am a bit new when it comes to C++ and I wasnt sure how to go about
    doing this. How does one compute values at compile which can be
    plugged into a static const value using a template. I remember seeing
    in a book I no longer have some method for doing this. For example the
    book gives a method for computing at compile time the value of
    Fibonacci sequence where the value is generated by the compiler.
    Unfortunately I seem to have misplaced the book.

    Thanks in advance,

    Carter.
    Carter, Mar 4, 2009
    #1
    1. Advertising

  2. Carter

    SG Guest

    Carter wrote:
    > I am a bit new when it comes to C++ and I wasnt sure how to go about
    > doing this. How does one compute values at compile which can be [...]


    http://en.wikipedia.org/wiki/Template_metaprogramming

    There's an example for compile-time factorial computation.

    Cheers!
    SG
    SG, Mar 4, 2009
    #2
    1. Advertising

  3. Carter

    Carter Guest

    On Mar 5, 12:13 am, Jeff Schwab <> wrote:
    > Carter wrote:
    > > I am a bit new when it comes to C++ and I wasnt sure how to go about
    > > doing this. How does one compute values at compile which can be
    > > plugged into a static const value using a template. I remember seeing
    > > in a book I no longer have some method for doing this. For example the
    > > book gives a method for computing at compile time the value of
    > > Fibonacci sequence where the value is generated by the compiler.
    > > Unfortunately I seem to have misplaced the book.

    >
    > See C++ Template Metaprogramming, by Abrahams and Gurtovoy.  Basically,
    > you just define a template to instantiate itself recursively, and
    > specialize for the base case.


    Thanks both.
    Carter, Mar 4, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    2
    Views:
    2,312
    joris
    Mar 24, 2010
  2. le Cl?
    Replies:
    15
    Views:
    3,133
    Michel
    Apr 11, 2009
  3. Jesper Sahner

    "Symbolic computation"

    Jesper Sahner, Oct 23, 2004, in forum: Java
    Replies:
    3
    Views:
    428
  4. Xiangliang Meng
    Replies:
    1
    Views:
    1,592
    Victor Bazarov
    Jun 21, 2004
  5. Jason Lillywhite

    display computation time in IRB

    Jason Lillywhite, Mar 20, 2009, in forum: Ruby
    Replies:
    4
    Views:
    115
    Joel VanderWerf
    Mar 20, 2009
Loading...

Share This Page