By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,671 Members | 1,633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,671 IT Pros & Developers. It's quick & easy.

class static variables & __STDC_VERSION__

P: n/a
Would some kind soul suggest a pre-processor test for the C++ language
revision whereby class static variables were specified to refer to the same
instance? Specifically, the following Singleton template will work with some
compilers but not with older ones (because every module that includes the
header gets its own unique static 'instance'):

template<typename T>
struct Singleton
{
static T& Instance() { static T instance; return instance; }

private:
Singleton() { }
~Singleton() { }

Singleton(const Singleton& rhs);
Singleton& operator=(const Singleton& rhs);
};
Can the standard pre-processor definition __STDC_VERSION__ be tested? If so,
for what value?

#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
:
#endif
Regards
Tim
Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
In article <3f*********************@news.dk.uu.net>,
no*******@nospamphaseone.nospamdk says...
Would some kind soul suggest a pre-processor test for the C++ language
revision whereby class static variables were specified to refer to the same
instance? Specifically, the following Singleton template will work with some
compilers but not with older ones (because every module that includes the
header gets its own unique static 'instance'):
[ ... ]
Can the standard pre-processor definition __STDC_VERSION__ be tested? If so,
for what value?


__STDC_VERSION__ doesn't seem to be defined in the standard at all.
__cplusplus must be defined to the value 199711L by a conforming
implementation, but there are no defined values for various versions of
non-conformance, and there's nothing to stop a non-conforming compiler
from defining it to the value claiming conformance.

To make a long story short, if the compiler doesn't define __cplusplus
to the value 199711L, then the compiler doesn't conform. If the
compiler _does_ define it to that value, it may or may not conform (but
probably still doesn't).

If it doesn't conform, there are (TTBOMK) no other typical values used
to specify the presence of particular features.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #2

P: n/a
Jerry Coffin wrote:
In article <3f*********************@news.dk.uu.net>,
no*******@nospamphaseone.nospamdk says...
Would some kind soul suggest a pre-processor test for the C++
language revision whereby class static variables were specified to
refer to the same instance? Specifically, the following Singleton
template will work with some compilers but not with older ones
(because every module that includes the header gets its own unique
static 'instance'):


[ ... ]
Can the standard pre-processor definition __STDC_VERSION__ be
tested? If so, for what value?


__STDC_VERSION__ doesn't seem to be defined in the standard at all.
__cplusplus must be defined to the value 199711L by a conforming
implementation, but there are no defined values for various versions
of non-conformance, and there's nothing to stop a non-conforming
compiler
from defining it to the value claiming conformance.

To make a long story short, if the compiler doesn't define __cplusplus
to the value 199711L, then the compiler doesn't conform. If the
compiler _does_ define it to that value, it may or may not conform
(but probably still doesn't).

If it doesn't conform, there are (TTBOMK) no other typical values used
to specify the presence of particular features.


Jerry,

I never knew __cplusplus had a value :-O So, this is about the best that can
be done then...

#if __cplusplus >= 199711
Thanks for the help
Tim
Jul 19 '05 #3

P: n/a
"Tim Clacy" <no*******@nospamphaseone.nospamdk> wrote in message
news:3f*********************@news.dk.uu.net...
I never knew __cplusplus had a value :-O So, this is about the best that can
be done then...

#if __cplusplus >= 199711


Some older implementations merely #define __cplusplus as an empty token sequence.
For maximum portability, you might favor:

#if __cplusplus + 0 >= 199711

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 19 '05 #4

P: n/a
P.J. Plauger wrote:
"Tim Clacy" <no*******@nospamphaseone.nospamdk> wrote in message
news:3f*********************@news.dk.uu.net...
I never knew __cplusplus had a value :-O So, this is about the best
that can be done then...

#if __cplusplus >= 199711


Some older implementations merely #define __cplusplus as an empty
token sequence. For maximum portability, you might favor:

#if __cplusplus + 0 >= 199711

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


Blimey... Mr Plauger of Microsoft STL fame?

Now I'm a little confused about the prepocessor rules; are you saying that
you can't compare an 'empty token' (just #define?) to a numeric constant
(like my naive effort), but you can add an 'empty token' to 0 to yield a
numeric type that can be compared to a numeric constant (like your
suggestion)?

Cheers
Tim
Jul 19 '05 #5

P: n/a
Tim Clacy wrote in news:3f*********************@news.dk.uu.net:
P.J. Plauger wrote:
"Tim Clacy" <no*******@nospamphaseone.nospamdk> wrote in message
news:3f*********************@news.dk.uu.net... Some older implementations merely #define __cplusplus as an empty
token sequence. For maximum portability, you might favor:

#if __cplusplus + 0 >= 199711
Now I'm a little confused about the prepocessor rules; are you saying
that you can't compare an 'empty token' (just #define?) to a numeric
constant (like my naive effort), but you can add an 'empty token' to 0
to yield a numeric type that can be compared to a numeric constant
(like your suggestion)?


The preprocessor does elementry (integer only ) arithmatic so:

#if __cplusplus + 0 >= 199711

will be expanded to:

#if + 0 >= 199711

or:

#if 199711 + 0 >= 199711

depending on how the compiler defines __cplusplus.

In the first example the + in "+ 0" is seen by the preprocessor
as a unary + so it evaluates "+ 0" as 0. In the second example
the + is binary + so the preporcessor does an addition.

So what is happing is a token expansion trick not some special
preprocessor addition rules being applied.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #6

P: n/a
Rob Williscroft wrote:
Tim Clacy wrote in news:3f*********************@news.dk.uu.net:
P.J. Plauger wrote:
"Tim Clacy" <no*******@nospamphaseone.nospamdk> wrote in message
news:3f*********************@news.dk.uu.net... Some older implementations merely #define __cplusplus as an empty
token sequence. For maximum portability, you might favor:

#if __cplusplus + 0 >= 199711

Now I'm a little confused about the prepocessor rules; are you saying
that you can't compare an 'empty token' (just #define?) to a numeric
constant (like my naive effort), but you can add an 'empty token' to
0 to yield a numeric type that can be compared to a numeric constant
(like your suggestion)?


The preprocessor does elementry (integer only ) arithmatic so:

#if __cplusplus + 0 >= 199711

will be expanded to:

#if + 0 >= 199711

or:

#if 199711 + 0 >= 199711

depending on how the compiler defines __cplusplus.

In the first example the + in "+ 0" is seen by the preprocessor
as a unary + so it evaluates "+ 0" as 0. In the second example
the + is binary + so the preporcessor does an addition.

So what is happing is a token expansion trick not some special
preprocessor addition rules being applied.

Rob.


Excellent; thanks.
Jul 19 '05 #7

P: n/a
Tim Clacy wrote:


Blimey... Mr Plauger of Microsoft STL fame?


That isn't very nice - there are sonmethings in a
distibguished career that are best forgoten
http://www.plauger.com/resume.html

Jul 19 '05 #8

P: n/a
Tim Clacy wrote:
Blimey... Mr Plauger of Microsoft STL fame?

What, you thought it was the science fiction writer? Oh wait . . .


Brian Rodenborn
Jul 19 '05 #9

P: n/a
Blimey... Mr Plauger of Microsoft STL fame?


Gee, I guess I'm older, I remember him as Mr. Plauger of the Whitesmiths
software license stamp fame.
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.