Static variable in class template - multiple definition

Discussion in 'C++' started by Hel, May 30, 2008.

  1. Hel

    Hel Guest

    Hi,

    I'm sure you are familiar with this problem:

    A.h:

    template <unsigned N> struct A {
    static const unsigned a;
    };

    template <> const unsigned A<0>::a = 1;
    template <unsigned N> const unsigned A<N>::a = A<N-1>::a*N;

    Now if I include A.h from multiple .cpp files I get a linking error
    because A<N> has multiple definitions. Easy solution there, move the
    definitions to A.cpp and just include A.h. BUT:

    B.h:

    #include "A.h"

    template <unsigned M> class B {
    int b[A<M>::a];
    };

    This only works if the compiler has seen the definition of A<N>::a. (g+
    + 4.2 says "array bound is not an integer constant".)

    I can't wrap my head around this one. I need the declaration for B<M>
    in many .cpp files, and I need to calculate A<M>::a in the declaration
    of B (in fact, not to define the size of the array, but as another
    template parameter - so dynamic allocation is not an option. The
    template definition is left out for brevity's sake.)

    TIA

    Helge
    Hel, May 30, 2008
    #1
    1. Advertising

  2. Hel

    Noah Roberts Guest

    Hel wrote:
    > Hi,
    >
    > I'm sure you are familiar with this problem:
    >
    > A.h:
    >
    > template <unsigned N> struct A {
    > static const unsigned a;
    > };
    >
    > template <> const unsigned A<0>::a = 1;
    > template <unsigned N> const unsigned A<N>::a = A<N-1>::a*N;
    >
    > Now if I include A.h from multiple .cpp files I get a linking error
    > because A<N> has multiple definitions.


    Because A<0>::a has multiple definitions actually. Try just moving that
    one.

    BTW, you might consider an alternative design, since you are dealing
    with integral values:

    template < unsigned N >
    struct A
    {
    enum { a = A<N-1>::a * N };
    };

    template <>
    struct A<0>
    {
    enum { a = 1 };
    };

    Numerous benefits here. For instance, I don't believe any of the A
    objects are actually instantiated and therefore, unlike your static int
    version that will instantiate all in between, there is only one static
    value that is generated by the metaprogram.
    Noah Roberts, May 30, 2008
    #2
    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. Jianli Shen
    Replies:
    1
    Views:
    567
    Victor Bazarov
    Mar 13, 2005
  2. BigMan
    Replies:
    3
    Views:
    1,418
    BigMan
    Apr 22, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    735
    Pete Becker
    May 4, 2005
  4. Pierre Yves
    Replies:
    2
    Views:
    473
    Pierre Yves
    Jan 10, 2008
  5. aaragon
    Replies:
    3
    Views:
    1,308
    Alf P. Steinbach
    Feb 21, 2009
Loading...

Share This Page