473,804 Members | 3,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me 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 2314
In article <3f************ *********@news. dk.uu.net>,
no*******@nospa mphaseone.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*******@nospa mphaseone.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*******@nosp amphaseone.nosp amdk> wrote in message
news:3f******** *************@n ews.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*******@nosp amphaseone.nosp amdk> wrote in message
news:3f******** *************@n ews.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******** *************@n ews.dk.uu.net:
P.J. Plauger wrote:
"Tim Clacy" <no*******@nosp amphaseone.nosp amdk> wrote in message
news:3f******** *************@n ews.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******** *************@n ews.dk.uu.net:
P.J. Plauger wrote:
"Tim Clacy" <no*******@nosp amphaseone.nosp amdk> wrote in message
news:3f******** *************@n ews.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
2444
by: Phil Powell | last post by:
Consider this: class ActionHandler { /*------------------------------------------------------------------------------------------------------------------------------------------------------------------- This class will be a singleton class structure by calling its constructor by reference only (prefixed by '&'). Is primarly used to reference its errorArray Array property as a single reference to allow for static usage
11
7694
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 __STDC_VERSION__ isn't defined. It obviously doesn't short circuit the expression, but evaluates both sides of the ||. I have, as a workaround, changed the statements into this:
9
2505
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, DT
4
1814
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 calls one of my static methods (not listed) for the first time. Any help is greatly appreciated public class MediaHelper { private static WindowsMediaPlayer wmp = new WindowsMediaPlayerClass();
9
2311
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 that the app uses. So I will need to change the fonts depending on what settings the user has entered. However, it seems kind of wasteful to me to go to teh registry, fetch the font information and create new font objects for every form that I am...
5
6780
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 (1) holds for unmanaged C++ code, but not for managed code. I have class library with both managed and unmanaged static variables that are not referenced by any part of the program. All the
5
3612
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 fine. 1 / Now If I move the getSpectrumAtDistance( const T &dist ) method definition in SpectralProfofile.cc let's say the compiler says core/profile.cc:199: error: `kNumBins' was not declared in this scope
8
8934
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. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
0
5020
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 : System.Web.HttpContext.Current.Session 2) For accesing Application variables : System.Web.HttpContext.Current.Application 3) For accesing QueryString variables : System.Web.HttpContext.Current.Request.QueryString Here is a simple example where...
0
10335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.