473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

class static variables & __STDC_VERSION__

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
9 2281
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
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
"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.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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Powell | last post by:
Consider this: class ActionHandler { ...
11
by: Bjørn Augestad | last post by:
Hi, I currently use a compiler which emits a warning on the following statement: #if defined(__STDC_VERSION__) || __STDC_VERSION__ < 19990601L .... #endif The compiler complains that...
9
by: vp | last post by:
Can I safely assume that all static variables are initialized as NULL or zero, depending on the types of the variables, no matter on which platform that app is compiled ? Thanks for your help, ...
4
by: TS | last post by:
I am trying to add an event handler for a static variable of a class and i can't figure out how to do so since the class is never instantiated. The wmp variable gets instantiated when my code...
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
0
by: bharathreddy | last post by:
Here I will given an example on how to access the session, application and querystring variables in an .cs class file. Using System.Web.HttpContext class. 1) For accesing session variables :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.