473,699 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4577
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
2475
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
18
5754
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 once and for all, if possible. Most severe is the problem illustrated by the code below. It's based on the "pluggable factory pattern" described in http://www.adtmag.com/joop/crarticle.asp?ID=1520
7
12453
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 data will be initialized if it is used across many translation unit, assume that it has constructor and needs dynamic initialization? I have blindly searched for the answer but I still not thoroughly
13
2577
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 thinks x is undefined. Could someone explain what's going on here? Why can't I pass a static const member by reference? This is how I compile it: g++ -g -Wall sample_main.C # g++ -v 4.0.1...
14
6013
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
5
2005
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
7859
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
8371
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 the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
4
5821
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 Element_common<L2, Side<2,2 { public:
0
8686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9033
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7748
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6533
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.