473,396 Members | 1,683 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,396 software developers and data experts.

definition of template static default-constructed data members impossible?

Hi,

I have a problem with the C++ spec 14.7.3.15 (see
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14761 for relevant
discussion).

It seems that the code (below) is impossible to get right, as you
cannot define a static data member of a template class if it ONLY has
a default-initializer...

the following code generates the following error:

$ g++ -Wall -W -pedantic -ansi -o test test.cpp
/tmp/ccnWckun.o(.text+0x11): In function `main':
: undefined reference to `foo<goo>::f'
collect2: ld returned 1 exit status

a real-life example of a 'goo' is boost::mutex, that can only be
default-constructed. I wanted it to be a static data member of a
template class, but I can't seem to define it. i can declare it just
fine, but not define.

Paul

struct goo
{
goo() : y(20) {}
int y;
private:
goo(const goo&);
};

template< typename S >
struct foo {
static goo f;
};

template< >
goo foo< goo >::f;

#include<iostream>

int main() {
foo<goo> x;
std::cerr << x.f.y << std::endl;
}
Jul 22 '05 #1
7 3571
"Paul" <el**********@yahoo.com> wrote...
I have a problem with the C++ spec 14.7.3.15 (see
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14761 for relevant
discussion).

It seems that the code (below) is impossible to get right, as you
cannot define a static data member of a template class if it ONLY has
a default-initializer...
That seems right (unfortunately). You could still use a pointer initialised
from dynamic memory or a reference to that pointer (if you don't mind a
small
memory leak):

template<typename S> struct foo {
static goo& f;
};

template<>
goo& foo<goo>::f = *(new goo());
[...]


V
Jul 22 '05 #2
I suppose thats a solution, but then you have to deal with pointers and
remember to destroy it.

I thought of a local static variable, ie

static Goo& goo() { static Goo g; return &g; }

course that means i have to call goo() if i want to access it.

This is unbelievable, don't many people hit this problem?
default-constructed-only classes aren't that uncommon.

Paul

Jul 22 '05 #3
Hi Paul,

unfortunately, I can't help you with this problem, but I have a question,
regarding this code snippet:
template< >
goo foo< goo >::f;


Usually, you would only write:
goo foo<goo>::f;

Whats's the leading template<> good for? Is this, because the concerning
declaration is still part of the class declaration or something like that?

Just curious.

Regards,
Matthias
Jul 22 '05 #4
wrote in news:11**********************@c13g2000cwb.googlegr oups.com in
comp.lang.c++:
I suppose thats a solution, but then you have to deal with pointers and
remember to destroy it.

I thought of a local static variable, ie

static Goo& goo() { static Goo g; return &g; }

course that means i have to call goo() if i want to access it.

This is unbelievable, don't many people hit this problem?
default-constructed-only classes aren't that uncommon.


Not really, you're only hitting the problem because you are explicitly
specializing a static member, usually the un-specialized version would
do fine:

template < typename S >
goo foo< S >::f;

The above is a defenition, unlike the uninitialized explicit
specialization in your original post that is only a declaration.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5
Matthias Käppler wrote in news:cp*************@news.t-online.com in
comp.lang.c++:
Hi Paul,

unfortunately, I can't help you with this problem, but I have a
question, regarding this code snippet:
template< >
goo foo< goo >::f;
Usually, you would only write:
goo foo<goo>::f;


That is how to define a member of an explicity specalized class.

The template <> syntax is used as the OP is explicitly specializing
a member of an unspecialized class.
Whats's the leading template<> good for? Is this, because the
concerning declaration is still part of the class declaration or
something like that?


Yep its something like that :).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6
I used to do it without template<> but it wouldn't compile with gcc
3.4.x (mingw32).

see here: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17445

Seems that it was always required, GCC has only just started enforcing
it.

Jul 22 '05 #7
Paul wrote in news:11*********************@z14g2000cwz.googlegro ups.com
in comp.lang.c++:
Rob Williscroft wrote:
Not really, you're only hitting the problem because you are explicitly
specializing a static member, usually the un-specialized version

would
do fine:

template < typename S >
goo foo< S >::f;

The above is a defenition, unlike the uninitialized explicit
specialization in your original post that is only a declaration.


Fantastic thanks, this works :)

But one question: I assume this creates a different instance per
templated instance, NOT that there is a single static instance for a
class of data members.
Right?


The will be a seperate instance of this member for every "S" that the
class template foo< S > is instantiated with.

If you want one instance shared between every instantiated type then
derive foo< S > from a base:

struct foo_base
{
static goo f;
};

goo foo_base::f; /* *NOT* in a header file ! */

template < typename S >
class foo : private foo_base
{
public:

using foo_base::f; /* hoist foo_base::f to public */
};

For the benefit of others like me, the above goes in a header file like
any other template...


Yes.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #8

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

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
4
by: Gianni Mariani | last post by:
I posted a gcc bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13332 here is the text: ----------------------------------------------------------- This code will compile fine but fail on...
3
by: BigMan | last post by:
Why cannot I define a member of an explicitly specialized class template out of the class template specialization: template< int i > struct a { static void Do( ); }; template< >
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
2
by: Marc Mutz | last post by:
Hi, I'm currently wondering if and how you can separate definition from declaration of a full template specialisation. I have a UnitTest class that has an _assertEqual member template that I...
5
by: Brent Ritchie | last post by:
Hello, This is my first attempt at template programming so please bear with me. This is what I have so far: Property.h -------------------------- #ifndef PROPERTY_H_ #define PROPERTY_H_
16
by: Hendrik Schober | last post by:
Hi, suppose we have template< typename T > struct X; and some specializations: template<>
5
by: ruebezaehler | last post by:
Hello, I should separate the definition and declaration of template code. This works fine for non-specialized templates. But I do not know how to do this for specialized templates. Example: ...
2
by: Hel | last post by:
Hi, I'm sure you are familiar with this problem: A.h: template <unsigned Nstruct A { static const unsigned a; };
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.