Thread: Static member initialization with class template

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    Static member initialization with class template

    Hello,

    currently i experience something that i don't understand.

    Suppose i have a class
    Code:
    #include <string>
    
    template<typename T>
    class ContainerClass
    {
    public:
        static T doSomething(T a, T b)
        {
            std::string name = ContainerClass<T>::someString;
            return doSomethingAgain(name, T);
        }
    
        static std::string someString;
    };
    All of this is in a header file. What i now did was to initialize the static member someString in my main.cpp file like:

    Code:
    std::string ContainerClass<float>::someString = std::string("test");
    std::string ContainerClass<double>::someString = std::string("test");
    I use VS2008. When compiling under Debug someString is set correctly, but when compiling for Release the someString doesn't seem to be set at all.

    How can this happen?

    Best regards, threahdead

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    someString is one thing, and you are explicitly specifying its definition twice.

    try
    Code:
    template< typename T > std::string ContainerClass< T >::someString = std::string("test");
    if you use a template other than those specified, the someString var should be undefined.
    Last edited by pYro; 04-27-2011 at 11:23 AM. Reason: clarity

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    SomeString is being specialized. And since SomeContainer<float> is different from SomeContainer<double>, this is valid C++.
    What is not valid, however, is that it should be:

    Code:
    template<> std::string ContainerClass<float>::someString = std::string("test");
    template<> std::string ContainerClass<double>::someString = std::string("test");
    Don't forget the beginning part to tell the compiler it's a specialization!
    This prints fine in both release and debug. If you try to use SomeContainer<T> where T is neither float nor double, you WILL get a linking error, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Thanks for your replies!
    I really didn't intend to specialize the template, and honestly i didn't event know i was doing it .

    I'll try to compile the code with the <T> parameter tomorrow.

    Best regards, threahdead

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by threahdead View Post
    I really didn't intend to specialize the template, and honestly i didn't event know i was doing it .
    In order to initialise static members of a template class, it is necessary to specialise the template. In your case, that is because each ContainerClass<T> has a different member named someString (for example, T may be float, or char, or foo, or ...) and each has to be defined once.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Quote Originally Posted by grumpy View Post
    In order to initialise static members of a template class, it is necessary to specialise the template. In your case, that is because each ContainerClass<T> has a different member named someString (for example, T may be float, or char, or foo, or ...) and each has to be defined once.
    But is
    Code:
    template< typename T > std::string ContainerClass< T >::someString = std::string("test");
    a specialization then? Because the <T> doesn't even specify a certain type.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    No, not a specialization. Specializations look exactly the way you were shown by Elysia.
    Last edited by whiteflags; 04-28-2011 at 12:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-06-2011, 01:37 PM
  2. Replies: 4
    Last Post: 08-29-2010, 04:33 PM
  3. how to define a static member in a template class
    By wanziforever in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2009, 04:44 AM
  4. Why initialization of static member is a must?
    By meili100 in forum C++ Programming
    Replies: 9
    Last Post: 04-11-2008, 05:22 PM
  5. Replies: 5
    Last Post: 03-01-2008, 02:05 PM