Evaluating Preprocessor

Discussion in 'C++' started by pinkfloydhomer@gmail.com, Nov 10, 2006.

  1. Guest

    We might use the normal preprocessor something like this

    #define WIDTH 42
    #define HEIGHT 100
    #define AREA (WIDTH*HEIGHT)

    To have "42*100" is inserted in the code whereever AREA is used. But
    does an evaluating preprocessor exist that can do some evaluation on
    expressions so that AREA becomes 4200?

    I know that most compilers will figure this out themselves anyway.

    And I know that I should use a const instead for this kind of thing.

    But in general (for instance with old (C) compilers that don't have
    const, or compilers that don't optimize very well), it would be nice to
    _know_ that an expression is evaluated before compile-time and runtime.


    In general, it would be nice to have a simple and clean turing complete
    language for compile time evaluation. I guess I can easily make a
    simple but powerful preprocessor in Python that can do this, also for
    C/C++ code. But maybe it already exist?

    /David
     
    , Nov 10, 2006
    #1
    1. Advertising

  2. Greg Guest

    wrote:
    > We might use the normal preprocessor something like this
    >
    > #define WIDTH 42
    > #define HEIGHT 100
    > #define AREA (WIDTH*HEIGHT)


    But hopefully you won't.

    > To have "42*100" is inserted in the code whereever AREA is used. But
    > does an evaluating preprocessor exist that can do some evaluation on
    > expressions so that AREA becomes 4200?


    No, although the preprocessor does evaluate arithmatic expressions in
    some contexts (such as, for an #if directive), most of the time it
    merely performs textual substitution.

    > I know that most compilers will figure this out themselves anyway.
    >
    > And I know that I should use a const instead for this kind of thing.


    Then why not use const? Doing so would guarantee a compile-time
    evaluation of 42 * 100:

    const int kWidth = 42;
    const int kHeight = 100;
    const int kArea = 42 * 100;

    Since the compiler needs a constant value for the symbol "kArea" it has
    no choice but to multiply 42 by 100 at compile time.

    > But in general (for instance with old (C) compilers that don't have
    > const, or compilers that don't optimize very well), it would be nice to
    > _know_ that an expression is evaluated before compile-time and runtime.


    This is a C++ newsgroup, so it seems odd to be asking the best practice
    for languages other than C++.

    And if you want to know whether 42 * 100 is evaluated at runtime or at
    compile time, then just examine a disassembly of a compiled binary to
    find out. Alternately you could ask your compiler maker whether the
    compiler performs "constant folding" as an optimization. And if it
    doesn't, find a better compiler.

    > In general, it would be nice to have a simple and clean turing complete
    > language for compile time evaluation. I guess I can easily make a
    > simple but powerful preprocessor in Python that can do this, also for
    > C/C++ code. But maybe it already exist?


    I fervently hope that you do have better uses for your time than to
    rewrite the C++ preprocessor. Besides, it has been done, only it's
    called "C++ template metaprogramming".

    template <int N> struct Height
    { static const int value = N; };

    template <int N> struct Width
    { static const int value = N; };

    template <class H, class W>
    struct Area
    {
    static const int value = H::value * W::value;
    };

    Area<Height<42>, Width<100> >::value; // 4200

    It's possible to perform much more sophisticated (though not
    necessarily any less verbose) compile-time calculations with C++
    metaprogramming. At least, for those so inclined.

    Greg
     
    Greg, Nov 10, 2006
    #2
    1. Advertising

  3. Greg Guest

    > #define WIDTH 42
    > #define HEIGHT 100
    > #define AREA (WIDTH*HEIGHT)


    But hopefully, cooler heads will prevail.

    > To have "42*100" is inserted in the code whereever AREA is used. But
    > does an evaluating preprocessor exist that can do some evaluation on
    > expressions so that AREA becomes 4200?


    No, although the preprocessor does evaluate arithmatic expressions in
    some contexts (such as, for an #if directive), most of the time it
    merely performs textual substitution.

    > I know that most compilers will figure this out themselves anyway.


    > And I know that I should use a const instead for this kind of thing.


    Then why not use const? Doing so would guarantee a compile-time
    evaluation of 42 * 100:

    const int kWidth = 42;
    const int kHeight = 100;
    const int kArea = kWidth * kHeight;

    Since the compiler needs a constant value for the symbol "kArea" it has

    no choice but to multiply 42 by 100 at compile time.

    > But in general (for instance with old (C) compilers that don't have
    > const, or compilers that don't optimize very well), it would be nice to
    > _know_ that an expression is evaluated before compile-time and runtime.


    This is a C++ newsgroup, so it seems odd to be asking the best practice

    for languages other than C++.

    And if you want to know whether 42 * 100 is evaluated at runtime or at
    compile time, then just examine a disassembly of a compiled binary to
    find out. Alternately you could ask your compiler maker whether the
    compiler performs "constant folding" as an optimization. And if it
    doesn't, find a better compiler.

    > In general, it would be nice to have a simple and clean turing complete
    > language for compile time evaluation. I guess I can easily make a
    > simple but powerful preprocessor in Python that can do this, also for
    > C/C++ code. But maybe it already exist?


    I fervently hope that you do have better uses for your time than to
    rewrite the C++ preprocessor. Besides, it has been done, only it's
    called "C++ template metaprogramming".

    template <int N> struct Height
    { static const int value = N; };

    template <int N> struct Width
    { static const int value = N; };

    template <class H, class W>
    struct Area
    {
    static const int value = H::value * W::value;
    };

    Area<Height<42>, Width<100> >::value; // 4200

    It's possible to perform much more sophisticated (though not
    necessarily any less verbose) compile-time calculations with C++
    metaprogramming. At least, for those so inclined.

    Greg
     
    Greg, Nov 10, 2006
    #3
  4. Greg Guest

    We might use the normal preprocessor something like this

    > #define WIDTH 42
    > #define HEIGHT 100
    > #define AREA (WIDTH*HEIGHT)


    But hopefully, cooler heads will prevail.

    > To have "42*100" is inserted in the code whereever AREA is used. But
    > does an evaluating preprocessor exist that can do some evaluation on
    > expressions so that AREA becomes 4200?


    No, although the preprocessor does evaluate arithmatic expressions in
    some contexts (such as, for an #if directive), most of the time it
    merely performs textual substitution.

    > I know that most compilers will figure this out themselves anyway.


    > And I know that I should use a const instead for this kind of thing.


    Then why not use const? Doing so would guarantee a compile-time
    evaluation of 42 * 100:

    const int kWidth = 42;
    const int kHeight = 100;
    const int kArea = kWidth * kHeight;

    Since the compiler needs a constant value for the symbol "kArea" it has
    no choice but to multiply 42 by 100 at compile time.

    > But in general (for instance with old (C) compilers that don't have
    > const, or compilers that don't optimize very well), it would be nice to
    > _know_ that an expression is evaluated before compile-time and runtime.


    This is a C++ newsgroup, so it seems odd to be asking the best practice
    for languages other than C++.

    And if you want to know whether 42 * 100 is evaluated at runtime or at
    compile time, then just examine a disassembly of a compiled binary to
    find out. Alternately you could ask your compiler maker whether the
    compiler performs "constant folding" as an optimization. And if it
    doesn't, find a better compiler.

    > In general, it would be nice to have a simple and clean turing complete
    > language for compile time evaluation. I guess I can easily make a
    > simple but powerful preprocessor in Python that can do this, also for
    > C/C++ code. But maybe it already exist?


    I fervently hope that you do have better uses for your time than to
    rewrite the C++ preprocessor. Besides, it has been done, only it's
    called "C++ template metaprogramming".

    template <int N> struct Height
    { static const int value = N; };

    template <int N> struct Width
    { static const int value = N; };

    template <class H, class W>
    struct Area
    {
    static const int value = H::value * W::value;
    };

    Area<Height<42>, Width<100> >::value; // 4200

    It's possible to perform much more sophisticated (though not
    necessarily any less verbose) compile-time calculations with C++
    metaprogramming. At least, for those so inclined.

    Greg
     
    Greg, Nov 10, 2006
    #4
  5. Greg Guest

    wrote:
    >We might use the normal preprocessor something like this


    > #define WIDTH 42
    > #define HEIGHT 100
    > #define AREA (WIDTH*HEIGHT)


    But hopefully, cooler heads will prevail.

    > To have "42*100" is inserted in the code whereever AREA is used. But
    > does an evaluating preprocessor exist that can do some evaluation on
    > expressions so that AREA becomes 4200?


    No, although the preprocessor does evaluate arithmatic expressions in
    some contexts (such as, for an #if directive), most of the time it
    merely performs textual substitution.

    > I know that most compilers will figure this out themselves anyway.
    > And I know that I should use a const instead for this kind of thing.


    Then why not use const? Doing so would guarantee a compile-time
    evaluation of 42 * 100:

    const int kWidth = 42;
    const int kHeight = 100;
    const int kArea = kWidth * kHeight;

    Since the compiler needs a constant value for the symbol "kArea" it has
    no choice but to multiply 42 by 100 at compile time.

    > But in general (for instance with old (C) compilers that don't have
    > const, or compilers that don't optimize very well), it would be nice to
    > _know_ that an expression is evaluated before compile-time and runtime.


    This is a C++ newsgroup, so it seems odd to be asking the best practice
    for languages other than C++.

    And if you want to know whether 42 * 100 is evaluated at runtime or at
    compile time, then just examine a disassembly of a compiled binary to
    find out. Alternately you could ask your compiler maker whether the
    compiler performs "constant folding" as an optimization. And if it
    doesn't, find a better compiler.

    > In general, it would be nice to have a simple and clean turing complete
    > language for compile time evaluation. I guess I can easily make a
    > simple but powerful preprocessor in Python that can do this, also for
    > C/C++ code. But maybe it already exist?


    I fervently hope that you do have better uses for your time than to
    rewrite the C++ preprocessor. Besides, it has been done, only it's
    called "C++ template metaprogramming".

    template <int N> struct Height
    { static const int value = N; };

    template <int N> struct Width
    { static const int value = N; };

    template <class H, class W>
    struct Area
    {
    static const int value = H::value * W::value;
    };

    Area<Height<42>, Width<100> >::value; // 4200

    It's possible to perform much more sophisticated (though not
    necessarily any less verbose) compile-time calculations with C++
    metaprogramming. At least, for those so inclined.

    Greg
     
    Greg, Nov 10, 2006
    #5
    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. Hagay Lupesko

    Evaluating url trust level

    Hagay Lupesko, Oct 19, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    379
    Hagay Lupesko
    Oct 19, 2003
  2. microsoft.private.windows.netserver.setup

    Evaluating Formulas in ASP.NET

    microsoft.private.windows.netserver.setup, Mar 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    390
    Steve C. Orr [MVP, MCSD]
    Mar 4, 2004
  3. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    vb functions evaluating on load

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Aug 3, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    454
    Kevin Spencer
    Aug 4, 2004
  4. Markus Becker
    Replies:
    4
    Views:
    754
    Markus Becker
    Mar 4, 2004
  5. Cronus
    Replies:
    1
    Views:
    717
    Paul Mensonides
    Jul 15, 2004
Loading...

Share This Page