static list in template

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terence

    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.siz e() << 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
  • Ivan Vecerina

    #2
    Re: static list in template

    news:9h7cqv0a4a k6frmreqkthluru 6n5u7bihf@4ax.c om...
    | 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
    --
    Ivan Vecerina - expert in medical devices, software - info, links, contact information, code snippets



    Comment

    Working...