"Non-constant" constant can't be used as template argument

Discussion in 'C++' started by Martin Magnusson, Oct 8, 2004.

  1. It seems that the following code is not valid, but I need to find a
    workaround. Both gcc and Comeau tells me that j can't be used as a
    template argument, even though it's declared as a const unsigned. Is
    there a way to get around this?

    template< unsigned M >
    class Tree
    {
    public:
    Tree()
    {
    for (unsigned i = 0; i < M; ++i)
    {
    const unsigned j = i;
    Compare<j> c; // j cannot be used as template argument
    }
    }

    private:
    template< unsigned N >
    class Compare
    {
    public:
    bool operator()( int i1, int i2 )
    { return i1 < i2; }
    };
    };

    int main()
    {
    Tree<3> t;
    return 0;
    }
     
    Martin Magnusson, Oct 8, 2004
    #1
    1. Advertising

  2. Martin Magnusson

    JKop Guest

    Martin Magnusson posted:

    > It seems that the following code is not valid, but I need to find a
    > workaround. Both gcc and Comeau tells me that j can't be used as a
    > template argument, even though it's declared as a const unsigned. Is
    > there a way to get around this?
    >
    > template< unsigned M >
    > class Tree
    > {
    > public:
    > Tree()
    > {
    > for (unsigned i = 0; i < M; ++i)
    > {
    > const unsigned j = i;
    > Compare<j> c; // j cannot be used as template argument
    > }
    > }
    >
    > private:
    > template< unsigned N >
    > class Compare
    > {
    > public:
    > bool operator()( int i1, int i2 )
    > { return i1 < i2; }
    > };
    > };
    >
    > int main()
    > {
    > Tree<3> t;
    > return 0;
    > }
    >



    j is not a compile time constant.

    -JKop
     
    JKop, Oct 8, 2004
    #2
    1. Advertising

  3. "Martin Magnusson" <> wrote in message
    news:1097221267.vPROieUgxzGXwAH8QTqyrg@teranews...
    > It seems that the following code is not valid, but I need to find a
    > workaround. Both gcc and Comeau tells me that j can't be used as a
    > template argument, even though it's declared as a const unsigned. Is
    > there a way to get around this?
    >



    The way round it is to use loop unrolling.

    template< unsigned M >
    class Tree
    {
    public:
    Tree()
    {
    UnrolledLoop<0, M>::execute();
    }
    };

    template< unsigned I, unsigned M >
    struct UnrolledLoop
    {
    static void execute()
    {
    Compare<I> c;
    UnrolledLoop<I + 1, M>::execute();
    }
    };


    template< unsigned I >
    struct UnrolledLoop<I, I>
    {
    static void execute()
    {
    }
    };

    This is completely untested but hopefully you get the idea.

    john
     
    John Harrison, Oct 8, 2004
    #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. nw
    Replies:
    0
    Views:
    316
  2. nguillot
    Replies:
    5
    Views:
    532
  3. Casey Hawthorne
    Replies:
    1
    Views:
    721
    Arne Vajhøj
    Mar 18, 2009
  4. Ted Byers
    Replies:
    23
    Views:
    425
    Peter J. Holzer
    Nov 15, 2008
  5. Rick C. Hodgin

    Non-constant constant strings

    Rick C. Hodgin, Jan 19, 2014, in forum: C Programming
    Replies:
    561
    Views:
    1,864
    David Brown
    Feb 24, 2014
Loading...

Share This Page