Sign in to follow this  

GCC 4 and templates

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have just made the move to GCC 4, and the transition has been fairly rough so far. Anyway, I just figured this out out, and decided to see if anyone had some useful wisdom about the C++ standard. Basically, my singleton code is a cut and paste job, identical to that of OGRE and Enginuity, here are the relevant lines:
template <class T>
class Singleton // Our singleton class
{
protected:
	static T * __singleton;
};
// then derive a class 'A' from Singleton<A>
class A : public Singleton<A>
{
};
// and then we initialize the __singleton value to NULL
A *Singleton<A>::__singleton = NULL;

This didn't compile anymore on GCC 4, so after much experimentation, I discovered this works:
template <> A *Singleton<A>::__singleton = NULL;
// instead of this:
A *Singleton<A>::__singleton = NULL;

Now, I would guess this has something to do with template instantiation order, but I would like to know if this a GCC 4 bug, or a valid part of the C++ standard (I haven't read it), that GCC has never previously enforced? Thanks, SwiftCoder

Share this post


Link to post
Share on other sites
I'm guessing standard. Simply put, setting a specifiic singleton static variable to a certain value is a specialization, for which one uses the template <> syntax.

Also, you may want to switch that line to:

template < typename T > T * Singleton< T >::__singleton = 0;

So that you get this variable for all versions.

Further, __singleton == worst name ever. Identifiers starting with a double underscore are reserved for the implementation to use wherever it pleases. Name it "instance" or something.

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
*snip*

Further, __singleton == worst name ever. Identifiers starting with a double underscore are reserved for the implementation to use wherever it pleases. Name it "instance" or something.

I believe the rule is actually that identifiers containing a double underscore or starting with an underscore followed by a capital letter are reserved for the implementation. Either way, it's still a bad name.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
Quote:
Original post by MaulingMonkey
*snip*

Further, __singleton == worst name ever. Identifiers starting with a double underscore are reserved for the implementation to use wherever it pleases. Name it "instance" or something.

I believe the rule is actually that identifiers containing a double underscore or starting with an underscore followed by a capital letter are reserved for the implementation. Either way, it's still a bad name.

Enigma


Symbols starting with a double underscore happen to contain a double underscore, so it'd be more of a "in addition," list. But if one's going to list a fuller list, one should include the full list - for C++, you missed one:

Quote:
MSDN
* any global-scope name beginning with _.
* any name beginning with _ followed by an upper-case letter.
* any name containing __.

Share this post


Link to post
Share on other sites
Interesting, my only question was because I never had to use the template < > syntax before, for instance on GCC 3.3 this compiled without problem:

template <Typename T>
class A
{
};

class B : public A<void>
{
};
// This doesn't compile anymore, I have to use
template <>
class B : public A<void>
{
};


Ah well.
I seem to remember this discussion about leading underscores coming up before, but actually I don't use double underscores normally, only single (not sure where those slipped in from).
Now, some people complain you shouldn't use even single underscores, but I happen to like it to differentiate private from public members.

Does any one know if the 'entire' C++ standard is published on the web anywhere, I feel I should really get around to at least skimming it?

Thanks,

SwiftCoder

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Interesting, my only question was because I never had to use the template < > syntax before, for instance on GCC 3.3 this compiled without problem:
*** Source Snippet Removed ***


You seriously have to type:

template<>
class derived : public base< that , takes , template , arguments >
{
};


?!? That would be one part that does not make sense to myself, as derived is not a template class itself.

Quote:
Ah well.
I seem to remember this discussion about leading underscores coming up before, but actually I don't use double underscores normally, only single (not sure where those slipped in from).
Now, some people complain you shouldn't use even single underscores, but I happen to like it to differentiate private from public members.

It comes up every now and then, and I'm one of those people who argues against the pre or postfixing of underscores to any symbol. I don't mind using_them_for_spaces, but I do hate using them to differentiate apples from _apples, or value from _value. It's my_apples, your_apples, value, and new_value, gosh darn it!!!
Quote:
Does any one know if the 'entire' C++ standard is published on the web anywhere, I feel I should really get around to at least skimming it?

I'm 99% certain no, and probably yes. If you ever find a link to the full C++ standard let me know!!!

[Edited by - MaulingMonkey on May 17, 2005 2:16:32 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Does any one know if the 'entire' C++ standard is published on the web anywhere, I feel I should really get around to at least skimming it?


The 1998 standard I have is INCITS+ISO+IEC+14882-1998.pdf.
The 2003 standard I have is INCITSISOIEC14882-2003.pdf.

They cost $18 each, but Google might save you some money.

Share this post


Link to post
Share on other sites

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this