table generation with recursive templates

Discussion in 'C++' started by W Karas, Apr 30, 2009.

  1. W Karas

    W Karas Guest

    I wrote this code to generate an intialized array of 256 32-bit words:

    typedef unsigned char u_int8;
    typedef unsigned long u_int32;

    template <unsigned Bits, class Start>
    struct TG
    {
    typedef TG<Bits - 1, Start> Sub_start1;
    typedef TG<Bits - 1, Sub_start1> Sub_start2;

    Sub_start1 s1;
    Sub_start2 s2;

    static const u_int8 Val1 = Sub_start2::Val1;
    static const u_int8 Val2 = Sub_start2::Val2;
    static const u_int8 Val3 = Sub_start2::Val3;
    static const u_int8 Val4 = Sub_start2::Val4;
    };

    struct TG_base
    {
    static const u_int8 Val1 = 17;
    static const u_int8 Val2 = 19;
    static const u_int8 Val3 = 23;
    static const u_int8 Val4 = 29;
    };

    template <class Start>
    struct TG<0, Start>
    {
    static const u_int32 Val =
    (((((Start::Val4 << 8) | Start::Val3) << 8) |
    Start::Val2) << 8) | Start::Val1;

    const u_int32 val;

    TG(void) : val(Val) { }

    static const u_int8 Val1 = static_cast<u_int8>(Start::Val1 +
    TG_base::Val1);
    static const u_int8 Val2 = static_cast<u_int8>(Start::Val2 +
    TG_base::Val2);
    static const u_int8 Val3 = static_cast<u_int8>(Start::Val3 +
    TG_base::Val3);
    static const u_int8 Val4 = static_cast<u_int8>(Start::Val4 +
    TG_base::Val4);
    };

    TG<8, TG_base> table_data;

    const u_int32 * const Table =
    reinterpret_cast<const u_int32 *>(&table_data);

    When I compiled with assembler output, I found that the table_data
    structure was not being statically initialized. I tried with two
    different compilers, with optimization enabled, and both generated a
    constructor with store instructions to initialize table_data . So I
    discarded the templates and used macros instead. Does anyone know of
    a compiler that would realize it could statically initialize
    table_data in this example?
    W Karas, Apr 30, 2009
    #1
    1. Advertising

  2. W Karas

    SG Guest

    On 30 Apr., 20:49, W Karas <> wrote:
    > I wrote this code to generate an intialized array of 256 32-bit words:
    > [...]
    > When I compiled with assembler output, I found that the table_data
    > structure was not being statically initialized.


    Right. I also spent a while trying to figure out if I could let the
    compiler generate a table statically via templates (or something
    else). I don't think it's possible.

    What I did instead was I wrote a little command line program that
    generated a file "tables.inc" (lookup tables in C++ syntax) which is
    just included in a C++ file. It's not difficult to write a proper
    Makefile for this. You just add a target "tables.inc" and some other
    dependencies. ;-)

    Cheers!
    SG
    SG, Apr 30, 2009
    #2
    1. Advertising

  3. W Karas

    Michael Mol Guest

    On Apr 30, 3:17 pm, SG <> wrote:
    > On 30 Apr., 20:49, W Karas <> wrote:
    >
    > > I wrote this code to generate an intialized array of 256 32-bit words:
    > > [...]
    > > When I compiled with assembler output, I found that the table_data
    > > structure was not being statically initialized.

    >
    > Right. I also spent a while trying to figure out if I could let the
    > compiler generate a table statically via templates (or something
    > else). I don't think it's possible.
    >
    > What I did instead was I wrote a little command line program that
    > generated a file "tables.inc" (lookup tables in C++ syntax) which is
    > just included in a C++ file. It's not difficult to write a proper
    > Makefile for this. You just add a target "tables.inc" and some other
    > dependencies. ;-)
    >
    > Cheers!
    > SG


    http://99-bottles-of-beer.net/language-c -108.html

    Extreme template metaprogramming. Go to town and scare your
    coworkers. :)
    Michael Mol, Apr 30, 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. JKop
    Replies:
    3
    Views:
    448
  2. recover
    Replies:
    2
    Views:
    782
    recover
    Jul 25, 2006
  3. n00m
    Replies:
    12
    Views:
    1,098
  4. vamsi
    Replies:
    21
    Views:
    2,041
    Keith Thompson
    Mar 9, 2009
  5. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    316
    John W. Long
    Nov 24, 2003
Loading...

Share This Page