public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* template instantiation and anonymous namespaces
@ 2009-07-14  8:05 Stefan Lampe
  2009-07-14 13:33 ` Ian Lance Taylor
  0 siblings, 1 reply; 2+ messages in thread
From: Stefan Lampe @ 2009-07-14  8:05 UTC (permalink / raw)
  To: gcc


Hi,

Here's some code that produced a surprising result with GCC 4.3.3 on linux 64. I'd have expected all addresses output to be the same.

I guess the reason for this is that when merging the templates instantiated in one.cpp and two.cpp, the linker doesn't perform a complete merge, but only merges for those methods that were instantiated in each translation unit. When using the zero argument constructor one type is used, when using the one argument constructor, another type is used.

As I understand it, the anonymous namespace has internal linkage, so Dummy should be considered a different type in each unit, and the template instantiations shouldn't really be merged. Under -O3 the addresses output are the same. Whether this is due to separate templates or a single merged template, I'm not sure.

I really just wanted to check that my understanding is correct. For the program I was working on I use named namespaces to provide the right semantics.

Thanks for your time,
Stefan

-- nonsingleton.hpp
 

#ifndef NON_SINGLETON
#define NON_SINGLETON

#include 

namespace {

class Dummy {};

}

template 
class Singleton {
    static ObjectT* object;
public:
    static ObjectT& Instance() {
        if (!object)
            object = new ObjectT();
        return *object; } };

template 
ObjectT* Singleton::object = 0;

class NonSingleton {
    typedef Singleton<> SingletonType;
public:
    NonSingleton() {
        std::cout << "0 arg constructor. Singleton: " << (&SingletonType::Instance()) << std::endl; }
    NonSingleton(int i) {
        std::cout << "1 arg constructor. Singleton: " << (&SingletonType::Instance()) << std::endl; }
    ~NonSingleton() {
        std::cout << "Destructor. Singleton: " << (&SingletonType::Instance()) << std::endl; } };

#endif

-- one.cpp

#include "nonsingleton.hpp"

void f() {
    NonSingleton a; }

-- two.cpp

#include "nonsingleton.hpp"

int main() {
    NonSingleton a;
    NonSingleton b(1);
    return 0; }

-- output

0 arg constructor. Singleton: 0x502010
1 arg constructor. Singleton: 0x502030
Destructor. Singleton: 0x502010
Destructor. Singleton: 0x502010


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

* Re: template instantiation and anonymous namespaces
  2009-07-14  8:05 template instantiation and anonymous namespaces Stefan Lampe
@ 2009-07-14 13:33 ` Ian Lance Taylor
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor @ 2009-07-14 13:33 UTC (permalink / raw)
  To: Stefan Lampe; +Cc: gcc

Stefan Lampe <stefanlampe@hotmail.com> writes:

> Here's some code that produced a surprising result with GCC 4.3.3 on
> linux 64. I'd have expected all addresses output to be the same.

This message should have gone to gcc-help@gcc.gnu.org rather than
gcc@gcc.gnu.org.  Please take any followups to gcc-help.  Thanks.

Your test case doesn't compile, so it's hard to be sure what is going
on.  The linker should normally merge template instantiations which have
the same name, but making the function static may be causing it to be
static in the file, and thus not merged.

Ian

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

end of thread, other threads:[~2009-07-14 13:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-14  8:05 template instantiation and anonymous namespaces Stefan Lampe
2009-07-14 13:33 ` Ian Lance Taylor

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