By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,025 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,025 IT Pros & Developers. It's quick & easy.

definition of template static default-constructed data members impossible?

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.