template static data member definition

Discussion in 'C++' started by ank, Jun 27, 2005.

  1. ank

    ank Guest

    Hi,
    I was curious about how to define static data member of template class.
    Should I put the definition in a separate source file or in the same
    header file as its template class?

    And when this data will be initialized if it is used across many
    translation unit, assume that it has constructor and needs dynamic
    initialization?

    I have blindly searched for the answer but I still not thoroughly
    understand this issue.
    ank, Jun 27, 2005
    #1
    1. Advertising

  2. ank

    titancipher Guest

    #include <stdio.h>

    template< typename T >
    class Test
    {
    public:
    Test( const T & id ) : m_id(id){ }
    private:
    T m_id;
    static int s_count;
    };

    int Test< float >::s_count = 0;

    int main( int argc, char *argv[] )
    {
    Test< float > test( 1.0f );

    return 0;
    }
    titancipher, Jun 27, 2005
    #2
    1. Advertising

  3. ank

    Guest

    > I was curious about how to define static data member of template class.
    > Should I put the definition in a separate source file or in the same
    > header file as its template class?


    Same headerfile, after the class.

    > And when this data will be initialized if it is used across many
    > translation unit, assume that it has constructor and needs dynamic
    > initialization?


    Same place, it will be initialised only once.
    For dynamic initialisation, fill your variable with dummy data then
    when the constructor gets called fill in the real values. You might
    want to add a static bool and an if statement around the initalisation
    in the constructor as a guard against multiple initialisations.

    > I have blindly searched for the answer but I still not thoroughly
    > understand this issue.


    What specific issue? How static works.
    Try 'Thinking in C++ volume 1' chapter 10
    (http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html)
    , Jun 27, 2005
    #3
  4. ank

    ank Guest

    wrote:
    >
    > Same place, it will be initialised only once.
    > For dynamic initialisation, fill your variable with dummy data then
    > when the constructor gets called fill in the real values. You might
    > want to add a static bool and an if statement around the initalisation
    > in the constructor as a guard against multiple initialisations.


    And what time will it be initialized? Before or after its use or
    unspecified just like normal static data member/global object.

    I don't know if it could be different from normal static storage
    external object.
    However, as template code will always be included in every translation
    unit (unlike the external object, which is not), I suspect it may be
    treated differently in the different compiler.

    > when the constructor gets called fill in the real values. You might
    > want to add a static bool and an if statement around the initalisation
    > in the constructor as a guard against multiple initialisations.


    I don't think that static external object will be initialized twice and
    it doesn't suffer from multithreading issue anyway, although it can be
    too late to initialize that global object before it is used.
    ank, Jun 27, 2005
    #4
  5. ank

    ank Guest

    Sorry if you feel like I am offending you, that's not my intention, but
    this doesn't seem to answer my question.

    My point is about the initialization time of the template static data
    member across translation unit. Is it just like normal static data
    member?

    I doubt this because the definition seems to be found before any use of
    it although I don't know if the template instantation always follow the
    order in which I see inside the code.
    ank, Jun 27, 2005
    #5
  6. ank

    Guest

    > And what time will it be initialized? Before or after its use or
    > unspecified just like normal static data member/global object.


    The same as any class member variable that has been declared static.

    > > when the constructor gets called fill in the real values. You might
    > > want to add a static bool and an if statement around the initalisation
    > > in the constructor as a guard against multiple initialisations.

    >
    > I don't think that static external object will be initialized twice and
    > it doesn't suffer from multithreading issue anyway, although it can be
    > too late to initialize that global object before it is used.


    I was wrong in my wording
    You do not initialise in the constructor, you assign the real values.
    Since you do not initialise you do need the guard to protect against
    multiple assignments to mimic the effect of static initialisation.

    Oh and you might want to take into account the following:
    MyTemplate<int> is a different class from MyTemplate<float>
    Which means that you also have two different statics member variables
    , Jun 27, 2005
    #6
  7. ank

    ank Guest

    What do you mean "not initialise in the constructor"?
    I really don't understand that.

    Constructor is responsible for that task, isn't it?

    Anyway, my initial guess is that template static data member is either
    initialized before the first use by any module or initialized in some
    module (and that may be too late for using it by other external static
    object).
    ank, Jun 27, 2005
    #7
  8. ank

    Guest

    ank wrote:
    > What do you mean "not initialise in the constructor"?
    > I really don't understand that.
    >
    > Constructor is responsible for that task, isn't it?


    Not in the case of static member variables. The class constructor is
    not called before or at the time that the static variable has to
    contain a value.
    Because you have to give the static variable a value before you have
    access to the real values you want to store in it you need to feed the
    variable dummy data.
    Then in the constructor of the class you assign real values, but to
    mimic static initialisation you need to add in a guard so that this
    assignment happens only once.

    >
    > Anyway, my initial guess is that template static data member is either
    > initialized before the first use by any module or initialized in some
    > module


    A static member of a template class is initialised at the same time any
    static member of a non template class is initialised. You have a
    guarantee it exists when
    template<typename T>
    VarType ClassName<T>::VarName = Value;
    in the header file is processed.

    > (and that may be too late for using it by other external static
    > object).


    Existence does not equal accessibility. Unless you have declared the
    static variable in the public section of the class, this external
    object cannot access your static variable until an object of the class
    containing the static variable exists. And even then only through the
    appropiate functions of the class.

    If you are stuck with static initialisation dependency (That is static
    object A has to exist before static object B) I again refer you to
    'Thinking in C++' vol 1, chapter 10. That chapter also has strategies
    on how to ensure proper initialisation.
    , Jun 28, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BigMan
    Replies:
    3
    Views:
    1,418
    BigMan
    Apr 22, 2005
  2. =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Replies:
    5
    Views:
    2,304
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Dec 14, 2006
  3. dolphin
    Replies:
    3
    Views:
    1,327
    Pete Becker
    Dec 5, 2007
  4. Jeffrey
    Replies:
    7
    Views:
    405
    James Kanze
    Sep 30, 2008
  5. aaragon
    Replies:
    3
    Views:
    1,308
    Alf P. Steinbach
    Feb 21, 2009
Loading...

Share This Page