public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/44855]  New: Static members not initialised in explicit template instances of library
@ 2010-07-07  7:17 hlprasu at gmail dot com
  2010-07-07  7:22 ` [Bug c++/44855] " hlprasu at gmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 10+ messages in thread
From: hlprasu at gmail dot com @ 2010-07-07  7:17 UTC (permalink / raw)
  To: gcc-bugs

Consider the following C++ source code files

=========> testLib.h <===========
#include <stdexcept>
#include <iostream>

#define DEBUG1(X) std::cout<<X;

typedef int IndexType;

namespace Upagraha
{

template<typename X,int Size>
class CompileTimeFixedSizeData
{

protected:
        X _data[Size];          //!< The storage of the data
        static IndexType _size; //!< The size of the data.
public:
        CompileTimeFixedSizeData()
        {
          DEBUG1("Template value = "<< Size << std::endl);
          DEBUG1("Static variable value = "<< _size<< std::endl);
        }

        virtual ~CompileTimeFixedSizeData()
        {}
};


//setting the value of _size, which is static
template<typename X,int Size>
IndexType CompileTimeFixedSizeData<X,Size>::_size=Size;

/** Create an instance of CompileTimeFixedSizeData and associated functions. 
 */
#define COMPILE_TIME_FIXED_SIZE_DATA_INSTANCE(TYPE,SIZE) \
        template class CompileTimeFixedSizeData<TYPE,SIZE>;

}

=========> testLib.cpp <===========
#include "testLib.h"
namespace Upagraha
{
        COMPILE_TIME_FIXED_SIZE_DATA_INSTANCE(double,3)
}

=========> main.cpp <===========
#include "testLib.h"

namespace Upagraha {
//extern template class CompileTimeFixedSizeData<double,3>;
// FIXME: Uncomment to see a change in behavior
}

int main()
{
        Upagraha::CompileTimeFixedSizeData<double,3> a;
}


=========> makefile <===========

#OPTIONS=-rdynamic -fno-implicit-templates -fPIC
OPTIONS=-rdynamic -fno-implicit-templates
# FIXME: Uncomment the top one to see change in behaviour

test:libtestLib.so testLib.h main.cpp
        g++ $(OPTIONS) $(TOPTIONS) main.cpp -L"${PWD}" -ltestLib -o main 

libtestLib.so: testLib.cpp testLib.h
        g++ $(OPTIONS) $(TOPTIONS) -shared testLib.cpp -o libtestLib.so

clean:
        rm main libtestLib.so *~

====================================

Put the above four files in same the directory and do the following
*) make
*) export LD_LIBRARY_PATH="$PWD"
*) ./main

You'll see the output as 
=====================
Template value = 3
Static variable value = 0
=====================

Now, follow either of the two "FIXME"s and build the program and run to get
=====================
Template value = 3
Static variable value = 3
=====================

Why this difference in behaviour in initialisation of static variable
Upagraha::CompileTimeFixedSizeData<X,Size>::_size? Could somebody please
explain this?


-- 
           Summary: Static members not initialised in explicit template
                    instances of library
           Product: gcc
           Version: 4.4.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: hlprasu at gmail dot com
 GCC build triplet: i686-redhat-linux
  GCC host triplet: i686-redhat-linux
GCC target triplet: i686-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44855


^ permalink raw reply	[flat|nested] 10+ messages in thread
[parent not found: <bug-44855-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2011-10-02 10:19 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-07  7:17 [Bug c++/44855] New: Static members not initialised in explicit template instances of library hlprasu at gmail dot com
2010-07-07  7:22 ` [Bug c++/44855] " hlprasu at gmail dot com
2010-07-07  9:18 ` redi at gcc dot gnu dot org
2010-07-07  9:30 ` hlprasu at gmail dot com
2010-07-25  2:04 ` pinskia at gcc dot gnu dot org
2010-07-25  2:14 ` pinskia at gcc dot gnu dot org
2010-07-25  6:35 ` hlprasu at gmail dot com
     [not found] <bug-44855-4@http.gcc.gnu.org/bugzilla/>
2011-03-29 19:51 ` redi at gcc dot gnu.org
2011-03-29 20:26 ` redi at gcc dot gnu.org
2011-10-02 10:19 ` paolo.carlini at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).