473,394 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Static variable in class template - multiple definition

Hel
Hi,

I'm sure you are familiar with this problem:

A.h:

template <unsigned Nstruct A {
static const unsigned a;
};

template <const unsigned A<0>::a = 1;
template <unsigned Nconst 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<Nhas multiple definitions. Easy solution there, move the
definitions to A.cpp and just include A.h. BUT:

B.h:

#include "A.h"

template <unsigned Mclass 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
Jun 27 '08 #1
2 4542
Hel wrote:
Hi,

I'm sure you are familiar with this problem:

A.h:

template <unsigned Nstruct A {
static const unsigned a;
};

template <const unsigned A<0>::a = 1;
template <unsigned Nconst 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<Nhas multiple definitions. Easy solution there, move the
definitions to A.cpp and just include A.h. BUT:

B.h:

#include "A.h"

template <unsigned Mclass 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.)
Since it's a static integral constant, you can simply initialise it in
the class itself:

template <unsigned Nstruct A {
static const unsigned a = A<N-1>::a * N;
};

And specialise the whole class instead of defining the member in the
header...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
Hel wrote:
Hi,

I'm sure you are familiar with this problem:

A.h:

template <unsigned Nstruct A {
static const unsigned a;
};

template <const unsigned A<0>::a = 1;
template <unsigned Nconst 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<Nhas 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.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
7
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
13
by: Amadeus W. M. | last post by:
I have a member static const int x defined in class Foo, and I'm passing it by reference, and by value elsewhere (see the code below). Passing it by value works, but by reference it doesn't: it...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
5
by: Gernot Frisch | last post by:
// - in my xy.cpp file -- template <const int Ttex, const int Tcol, const int Tlight, int TzBuffer> struct MyFragmentShader { static const int varying_count = Ttex*2 + Tcol*3 + Tlight; }; ...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.