Can a template class have a static data member of type T?

Discussion in 'C++' started by William Payne, Oct 11, 2004.

  1. As the title says, is something like this legal:

    template<typename T>
    class foo
    {
    public:
    static T* t;
    };

    T* foo::t;

    ?

    I can't get it to compile, it doesn't like my definition of the static
    member variable (I have it in the .cpp-file of the class). The compiler
    simply says parse error before * token.

    / WP
     
    William Payne, Oct 11, 2004
    #1
    1. Advertising

  2. William Payne wrote:
    > As the title says, is something like this legal:
    >
    > template<typename T>
    > class foo
    > {
    > public:
    > static T* t;
    > };
    >
    > T* foo::t;


    No. 'T' is undefined. Change this to

    template<typename T> T* foo<T>::t;

    >
    > ?
    >
    > I can't get it to compile, it doesn't like my definition of the static
    > member variable (I have it in the .cpp-file of the class). The compiler
    > simply says parse error before * token.


    Of course.

    Victor
     
    Victor Bazarov, Oct 11, 2004
    #2
    1. Advertising

  3. "William Payne" <> wrote in message
    news:ckedrq$9ed$...
    > As the title says, is something like this legal:
    >
    > template<typename T>
    > class foo
    > {
    > public:
    > static T* t;
    > };
    >
    > T* foo::t;
    >
    > ?
    >


    Like this

    template <typename T>
    T* foo::t;

    john
     
    John Harrison, Oct 11, 2004
    #3
  4. * William Payne:
    > As the title says, is something like this legal:
    >
    > template<typename T>
    > class foo
    > {
    > public:
    > static T* t;
    > };


    OK.


    > T* foo::t;


    Should be

    template<typename T>
    T* foo::t = something;


    > I can't get it to compile, it doesn't like my definition of the static
    > member variable (I have it in the .cpp-file of the class).


    Unless T is restricted to one or a few types better place that definition
    in the header file (and yes, that's supported by the standard).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 11, 2004
    #4
  5. John Harrison wrote:
    > "William Payne" <> wrote in message
    > news:ckedrq$9ed$...
    >
    >>As the title says, is something like this legal:
    >>
    >>template<typename T>
    >>class foo
    >>{
    >>public:
    >> static T* t;
    >>};
    >>
    >>T* foo::t;
    >>
    >>?
    >>

    >
    >
    > Like this
    >
    > template <typename T>
    > T* foo::t;
    >
    > john
    >
    >


    Thanks John, Victor, and Alf for your quick reply. The solution makes
    perfect sense to me because you have to do the same thing when defining
    member function outside the (templated) class declaration).
    I will soon be back, I think, with another question I see looming on the
    horizon now that I can continue working on this program.

    / WP
     
    William Payne, Oct 11, 2004
    #5
  6. William Payne wrote:

    > John Harrison wrote:
    >
    >> "William Payne" <> wrote in message
    >> news:ckedrq$9ed$...
    >>
    >>> As the title says, is something like this legal:
    >>>
    >>> template<typename T>
    >>> class foo
    >>> {
    >>> public:
    >>> static T* t;
    >>> };
    >>>
    >>> T* foo::t;
    >>>
    >>> ?
    >>>

    >>
    >>
    >> Like this
    >>
    >> template <typename T>
    >> T* foo::t;
    >>
    >> john
    >>
    >>

    >
    > Thanks John, Victor, and Alf for your quick reply. The solution makes
    > perfect sense to me because you have to do the same thing when defining
    > member function outside the (templated) class declaration).
    > I will soon be back, I think, with another question I see looming on the
    > horizon now that I can continue working on this program.
    >
    > / WP


    Hmm, I was a bit too quick to reply, it seems. I could only get Victor's
    variant to compile:

    template<typename T>
    T* foo<T>::t;

    Furthermore, I would get link errors if this definition wasn't in the
    headers, but that applies to member functions as well (export, where are
    you?).

    / WP
     
    William Payne, Oct 11, 2004
    #6
  7. * Jonathan Turkanis:
    >
    > "Alf P. Steinbach" <> wrote in message
    > news:...
    > > * William Payne:
    > > > As the title says, is something like this legal:
    > > >
    > > > template<typename T>
    > > > class foo
    > > > {
    > > > public:
    > > > static T* t;
    > > > };

    > >
    > > OK.
    > >
    > >
    > > > T* foo::t;

    > >
    > > Should be
    > >
    > > template<typename T>
    > > T* foo::t = something;

    >
    > I think
    >
    > template<typename T>
    > T* foo<T>::t;
    >
    > is okay. Scalar types of static storage duration are zero-initialized.


    Sorry for typo (forgot the "<T>" there).

    Re initialization or not, initializing namespace level variables is IMO a Good
    Habit.

    I never remember the rules of declaration versus definition but an initialized
    thing is definitely a definition, at least in C++.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 11, 2004
    #7
  8. "Alf P. Steinbach" <> wrote in message
    news:...
    > * William Payne:
    > > As the title says, is something like this legal:
    > >
    > > template<typename T>
    > > class foo
    > > {
    > > public:
    > > static T* t;
    > > };

    >
    > OK.
    >
    >
    > > T* foo::t;

    >
    > Should be
    >
    > template<typename T>
    > T* foo::t = something;


    I think

    template<typename T>
    T* foo<T>::t;

    is okay. Scalar types of static storage duration are zero-initialized.

    Jonathan
     
    Jonathan Turkanis, Oct 11, 2004
    #8
  9. William Payne wrote:
    > William Payne wrote:
    >
    >> John Harrison wrote:
    >>
    >>> "William Payne" <> wrote in message
    >>> news:ckedrq$9ed$...
    >>>
    >>>> As the title says, is something like this legal:
    >>>>
    >>>> template<typename T>
    >>>> class foo
    >>>> {
    >>>> public:
    >>>> static T* t;
    >>>> };
    >>>>
    >>>> T* foo::t;
    >>>>
    >>>> ?
    >>>>
    >>>
    >>>
    >>> Like this
    >>>
    >>> template <typename T>
    >>> T* foo::t;
    >>>
    >>> john
    >>>
    >>>

    >>
    >> Thanks John, Victor, and Alf for your quick reply. The solution makes
    >> perfect sense to me because you have to do the same thing when
    >> defining member function outside the (templated) class declaration).
    >> I will soon be back, I think, with another question I see looming on
    >> the horizon now that I can continue working on this program.
    >>
    >> / WP

    >
    >
    > Hmm, I was a bit too quick to reply, it seems. I could only get Victor's
    > variant to compile:
    >
    > template<typename T>
    > T* foo<T>::t;
    >
    > Furthermore, I would get link errors if this definition wasn't in the
    > headers,


    Thatz how the current state-of-the-art is. You have to define
    the templates in the header file.


    > but that applies to member functions as well (export, where are
    > you?).
    >
    > / WP


    For that matter, export keyword is supported by *very few*
    C++ compilers. Comeau compiler supports it.
    This document provides interesting reading regarding admitting
    'export' keyword in the standard.

    http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1426.pdf .

    --
    Karthik. http://akktech.blogspot.com .
    ' Remove _nospamplz from my email to mail me. '
     
    Karthik Kumar, Oct 11, 2004
    #9
    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. Siemel Naran
    Replies:
    4
    Views:
    808
    Micah Cowan
    Jan 12, 2005
  2. Replies:
    5
    Views:
    699
    Mark P
    Apr 4, 2006
  3. Frederiek
    Replies:
    1
    Views:
    382
    Thomas Tutone
    Sep 14, 2006
  4. Robin
    Replies:
    0
    Views:
    418
    Robin
    Jun 6, 2007
  5. dolphin
    Replies:
    3
    Views:
    1,348
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page