static member initialization in templates

Discussion in 'C++' started by gbs, Sep 11, 2004.

  1. gbs

    gbs Guest

    Isn't this legal code to initialize a static member of a template
    class?

    template <class T>
    class TemplatedClassT
    {
    static int staticMember_;
    };


    class A
    {
    //....
    };

    //...
    //...initialize
    //...
    int TemplatedClassT<A>::staticMember_ = 0;

    GCC 3.4.1 is rejecting it. But, how else could
    TemplatedClassT<A>::staticMember_ be initialized?

    Greg Silverman
    Northrop Grumman
    San Jose, CA

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    gbs, Sep 11, 2004
    #1
    1. Advertising

  2. gbs wrote:
    > Isn't this legal code to initialize a static member of a template
    > class?
    >
    > template <class T>
    > class TemplatedClassT
    > {
    > static int staticMember_;
    > };
    >
    >
    > class A
    > {
    > //....
    > };
    >
    > //...
    > //...initialize
    > //...
    > int TemplatedClassT<A>::staticMember_ = 0;
    >
    > GCC 3.4.1 is rejecting it. But, how else could
    > TemplatedClassT<A>::staticMember_ be initialized?
    >


    Are you 100% sure that the initialization happens only once,
    ie. that the file containing
    int TemplatedClassT<A>::staticMember_ = 0;
    is included only once?

    HTH,
    - J.
     
    Jacek Dziedzic, Sep 11, 2004
    #2
    1. Advertising

  3. Jacek Dziedzic wrote in news:chv01o$2pk$ in
    comp.lang.c++:

    > gbs wrote:
    >> Isn't this legal code to initialize a static member of a template
    >> class?
    >>
    >> template <class T>
    >> class TemplatedClassT
    >> {
    >> static int staticMember_;
    >> };
    >>
    >>
    >> class A
    >> {
    >> //....
    >> };
    >>


    The following is an explicit specialization (of a member),
    It should be preceded with:

    template <>

    >> int TemplatedClassT<A>::staticMember_ = 0;
    >>
    >> GCC 3.4.1 is rejecting it. But, how else could
    >> TemplatedClassT<A>::staticMember_ be initialized?
    >>

    >
    > Are you 100% sure that the initialization happens only once,
    > ie. that the file containing
    > int TemplatedClassT<A>::staticMember_ = 0;
    > is included only once?
    >


    test.cpp:20: error: too few template-parameter-lists
    test.cpp:20: error: expected `,' or `;' before '=' token

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Sep 11, 2004
    #3
  4. X-ReplaceAddress:yes
    gbs wrote:

    > Isn't this legal code to initialize a static member of a template
    > class?
    >
    > template <class T>
    > class TemplatedClassT
    > {
    > static int staticMember_;
    > };


    template<typename T>
    int TemplatedClassT<T>::staticMember_ = 0;

    Falk Tannhäuser

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    =?ISO-8859-1?Q?Falk_Tannh=E4user?=, Sep 12, 2004
    #4
  5. gbs

    Greg Comeau Guest

    In article <>,
    gbs <> wrote:
    >Isn't this legal code to initialize a static member of a template
    >class?
    >
    >template <class T>
    >class TemplatedClassT
    >{
    > static int staticMember_;
    >};
    >
    >
    >class A
    >{
    >//....
    >};
    >
    >//...
    >//...initialize
    >//...
    >int TemplatedClassT<A>::staticMember_ = 0;
    >
    >GCC 3.4.1 is rejecting it. But, how else could
    >TemplatedClassT<A>::staticMember_ be initialized?


    By prefixing it with 'template <>'
    --
    Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Greg Comeau, Sep 12, 2004
    #5
  6. gbs wrote:
    > Isn't this legal code to initialize a static member of a template
    > class?


    No.

    > template <class T>
    > class TemplatedClassT
    > {
    > static int staticMember_;
    > };
    >
    >
    > class A
    > {
    > //....
    > };
    >
    > //...
    > //...initialize
    > //...
    > int TemplatedClassT<A>::staticMember_ = 0;


    You must begin every full specialisation with "template<>". Just add
    that at the beginning of this declaration.

    > GCC 3.4.1 is rejecting it. But, how else could
    > TemplatedClassT<A>::staticMember_ be initialized?


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Ben Hutchings, Sep 12, 2004
    #6
    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. JKop
    Replies:
    3
    Views:
    479
  2. Replies:
    3
    Views:
    849
    Peter_Julian
    Oct 10, 2005
  3. recover
    Replies:
    2
    Views:
    812
    recover
    Jul 25, 2006
  4. dolphin
    Replies:
    3
    Views:
    1,349
    Pete Becker
    Dec 5, 2007
  5. aaragon
    Replies:
    2
    Views:
    621
    James Kanze
    Nov 2, 2008
Loading...

Share This Page