static list in template

T

Terence

Hi,

I created the following two files for testing:

// begin test.h
#include <list>

class E {};
struct L {};

template <class T> struct A {
static std::list<A<T> *> alist;

A() {}
A(const E &) { alist.push_back(this); }
};

struct B : public A<L>{
B(const E &e) : A<L>(e) {}
};
// end test.h

and:

// begin test.cpp
#include <iostream>
#include "test.h"
using namespace std;

template <class T> std::list<A<T> *> A<T>::alist;

template class A<L>;

static B *b_ex = new B(E());

int main() {
cout << A<L>::alist.size() << endl;
return 0;
}
// end test.cpp

Now, after compiling without errors or warnings, on execution I get a
segmentation fault concerning insertion in the list.

How could this problem be resolved, most genericly?

Thanks for all hints,

Terence
 
I

Ivan Vecerina

| I created the following two files for testing:
|

Hi Terence,

| // begin test.h
....
| template <class T> struct A {
| static std::list<A<T> *> alist;
....
| A(const E &) { alist.push_back(this); }
| };
|
| struct B : public A<L>{
| B(const E &e) : A<L>(e) {}
| };
....
| template <class T> std::list<A<T> *> A<T>::alist;
....
| static B *b_ex = new B(E());
....
| Now, after compiling without errors or warnings, on execution I get a
| segmentation fault concerning insertion in the list.
|
| How could this problem be resolved, most genericly?

This looks like a problem with initialization order:
b_ex is constructed before A<L>::alist,
which is used during the call to new B(...).

A workaround would be to use some kind of factory
function to create 'alist'. The simplest way to
do so is to replace (within the definition of A<T>):
static std::list<A<T> *> alist;
with:
static std::list<A<T> *> alist()
{
static std::list<A<T> *> instance;
return instance;
}

More sophisticated approaches may use some form
of reference counting. Special care also may have
to be taken within multithreaded applications...


I hope this helps,
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,828
Members
47,374
Latest member
anuragag27

Latest Threads

Top