public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Explicit instantiation and static objects in different modules
@ 2015-08-04 15:00 Nikolay Vorobyov
  2015-08-04 15:45 ` Jonathan Wakely
  0 siblings, 1 reply; 6+ messages in thread
From: Nikolay Vorobyov @ 2015-08-04 15:00 UTC (permalink / raw)
  To: gcc-help

Hi,

I use gcc-5.1.0 on Windows (x86_64-posix-seh-rev0, Built by MinGW-W64 project). I have the following template class:

template <class T>
struct StaticObject
{
   static T & getInstance()
   {
     static T t;
     std::cout << "static object " << typeid(T).name() << " at " << &t << " = "<< t <<std::endl;
     return t;
   }
};

What i want to achieve is to have one instance of say StaticObject<int> through my EXE and several DLLs modules with use of explicit instantiation.
So in one of my DLLs (inst.dll) i have explicit instantiation of StaticObject<int> - inst.cpp:

#include "static_object.h"

template struct StaticObject<int>;

And there is #include “externs.h” in each source file that use this static object to prevent it from implicit instantiation - externs.h :

#pragma once

#include "static_object.h"

extern template struct StaticObject<int>;

There is calls to StaticObject<int>::getInstance() in EXE and DLL modules and what i get is different behavior with optimisation turned on and off. 
It works as expected:
1) with optimisation turned off and 
2) with following flags: “-Ox -fno-inline”, where ‘x’ is one of optimisation levels 1,2,3.

And it doesn’t work with any (1,2,3) level of optimisation turned on, so i get instances of StaticObject<int> in each DLL and EXE.

Please see my github project for reference - https://github.com/dlardi/mingw_static_test

Is this a gcc's bug?

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-08-05 17:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-04 15:00 Explicit instantiation and static objects in different modules Nikolay Vorobyov
2015-08-04 15:45 ` Jonathan Wakely
2015-08-04 16:53   ` Alexander Monakov
2015-08-04 17:06     ` Jonathan Wakely
2015-08-04 17:32       ` Alexander Monakov
2015-08-05 17:22         ` Nikolay Vorobyov

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).