public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Nikolay Vorobyov <nik.vorobyov@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Explicit instantiation and static objects in different modules
Date: Tue, 04 Aug 2015 15:00:00 -0000	[thread overview]
Message-ID: <63FDE6C4-EFC2-44DD-991E-0BE895586162@gmail.com> (raw)

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?

             reply	other threads:[~2015-08-04 15:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-04 15:00 Nikolay Vorobyov [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=63FDE6C4-EFC2-44DD-991E-0BE895586162@gmail.com \
    --to=nik.vorobyov@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).