class static variables & __STDC_VERSION__

T

Tim Clacy

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
 
J

Jerry Coffin

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

Tim Clacy

Jerry said:
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
 
P

P.J. Plauger

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
 
T

Tim Clacy

P.J. Plauger said:
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
 
R

Rob Williscroft

Tim Clacy wrote in
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.
 
T

Tim Clacy

Rob said:
Tim Clacy wrote in


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top