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<N1>::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<N1>::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.
