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

static list in template

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
Jul 19 '05 #1
1 5666
news:9h********************************@4ax.com...
| 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
--
http://ivan.vecerina.com
Jul 19 '05 #2

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

Similar topics

7
by: SC Shin | last post by:
Hi, everyone I've got problem. I am able to compile following code without error on Compaq, Linux machine But I am not able to compile this with error on Sun with forte7 compiler: line 26:...
4
by: cass | last post by:
Both class A and B inherites from class C. A std::list is used to contain a list of objects of class A or a list of objects of class B, but not both types of classes at the same time. How to...
1
by: | last post by:
I think, a class template with a static vector member: ---------------------------- template <class C> struct Shared { unsigned int count; C data; }; template <class C>
3
by: Marco Jez | last post by:
The following code reproduces a problem I'm having with the GCC compiler. It compiles fine on MSVC, so I'm wondering whether it is legal C++ code or not. template<typename C> struct Provider {...
2
by: Arka | last post by:
Hi, I am trying to migrate a VS2003 VC++ Solution to VS2005. I found a problem with the list template ! I declared my list like this: class MyOwnTypeList : public list<MyOwnType::Ptr>
0
by: cakeathon | last post by:
I am trying to find a simplified template example of the STL list container on the web without much success. Please can some one point me in the right direction ?
3
by: Chris Forone | last post by:
hello group, whats wrong with following static member template: class x { template<typename InputIterator, typename std::valarray<char>&> static void Copy(InputIterator file,...
1
by: Angus | last post by:
Hi all I have a design which models a telephone system. Roughly the design has these two items: device class which models an extension on the telephone system. The device constructor takes...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.