Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Static variable in class template - multiple definition

Reply
Thread Tools

Static variable in class template - multiple definition

 
 
Hel
Guest
Posts: n/a
 
      05-30-2008
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
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      05-30-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
definition of static member variables of a class template aaragon C++ 3 02-21-2009 02:07 AM
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Out-of-class definition of a non-template member of an explicitly specialized class template? BigMan C++ 3 04-22-2005 06:28 AM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM



Advertisments