Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Non-const static members in templates?

Reply
Thread Tools

Non-const static members in templates?

 
 
Derek
Guest
Posts: n/a
 
      01-19-2005
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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-19-2005
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
 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      01-19-2005
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


 
Reply With Quote
 
Derek
Guest
Posts: n/a
 
      01-20-2005
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.

 
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
Thread safety problems with function scope static variables vs class static private members Hicham Mouline C++ 5 12-19-2008 08:10 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57