RE: template instantiation and anonymous namespaces

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Right, yes, I see what you're saying. The anonymous namespace makes the typedefs in the class different, so they are the same class name, but with different definitions. 

It seems Eljay's guideline of not using anonymous namespaces in header files is a good idea :)

Thanks,
Stefan

----------------------------------------
> Date: Wed, 15 Jul 2009 10:26:15 -0400
> From: johnsfine@xxxxxxxxxxx
> To: stefanlampe@xxxxxxxxxxx
> CC: gcc-help@xxxxxxxxxxx
> Subject: Re: template instantiation and anonymous namespaces
>
> I haven't tried putting that correction together with your original
> code, but I think I understand the major problem in doing so.
>
> You have made Singleton a templated class, so each compilation unit has
> a different instantiation of Singleton, but NonSingleton is not a
> templated class. You have included the definition of that one class in
> multiple compilation units and then linked them together, so your code
> is incorrect unless the class definitions of NonSingleton in those
> multiple compilation units are the same.
>
> You have made it so those class definitions have identical source code
> but do not compile into identical class definitions.
>
> That is just as much an error as if you had included different source
> code for defining the same class.
>
> Stefan Lampe wrote:
>> There's still a problem with the template parameters. Let me give the relevant class one last try with escapes:
>>
>> template
>> class Singleton {
>> static ObjectT* object;
>> public:
>> static ObjectT& Instance() {
>> if (!object)
>> object = new ObjectT();
>> return *object; } };
>>
>> template
>> ObjectT* Singleton::object = 0;
>>
>>
>>
>>
>


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux