473,486 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Non-const static members in templates?

Here is a simple template with a non-const static data member:

template <typename T>
class Foo
{
public:
static int bar;
};

template <typename T> int Foo<T>::bar = 42;

What I want to know is how the compiler/linker guarantees there
will only be one Foo<T>::bar for each possible T? I assume the
compiler generates storage for Foo<T>::bar in every translation
unit that instantiates Foo<T> and tasks the linker with stripping
any duplicates?
Jul 22 '05 #1
3 1185
Derek wrote:
Here is a simple template with a non-const static data member:

template <typename T>
class Foo
{
public:
static int bar;
};

template <typename T> int Foo<T>::bar = 42;

What I want to know is how the compiler/linker guarantees there
will only be one Foo<T>::bar for each possible T? I assume the
compiler generates storage for Foo<T>::bar in every translation
unit that instantiates Foo<T> and tasks the linker with stripping
any duplicates?


Probably. How does the compiler guarantee that there is only one
instance of a member function, for example? The same way, most likely.

If you really want to know the inner workings of a compiler, I'd look
at the source for G++ (it's open source, isn't it?) or asked in
comp.compilers.

V
Jul 22 '05 #2
Derek wrote:
Here is a simple template with a non-const static data member:

template <typename T>
class Foo
{
public:
static int bar;
};

template <typename T> int Foo<T>::bar = 42;

What I want to know is how the compiler/linker guarantees there
will only be one Foo<T>::bar for each possible T? I assume the
compiler generates storage for Foo<T>::bar in every translation
unit that instantiates Foo<T> and tasks the linker with stripping
any duplicates?


There is a good discussion in C++ Templates: The Complete Guide, starting on p.
153 ("Implementation Schemes")

Jonathan
Jul 22 '05 #3
Jonathan Turkanis wrote:
Here is a simple template with a non-const static data member:

template <typename T>
class Foo
{
public:
static int bar;
};

template <typename T> int Foo<T>::bar = 42;

What I want to know is how the compiler/linker guarantees there
will only be one Foo<T>::bar for each possible T? I assume the
compiler generates storage for Foo<T>::bar in every translation
unit that instantiates Foo<T> and tasks the linker with stripping
any duplicates?


There is a good discussion in C++ Templates: The Complete Guide,
starting on p. 153 ("Implementation Schemes")


Thanks for the tip, Jonathan. My library at work has a copy of
"C++ Templates" and I'm reading that section right now.

Jul 22 '05 #4

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

Similar topics

12
4395
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
12184
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
7537
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4464
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
11
3393
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
6087
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
2316
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
12625
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
9
3784
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
29787
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
7099
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,...
0
6964
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
7123
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
7319
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...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
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 ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.