class static variables & __STDC_VERSION__

Discussion in 'C++' started by Tim Clacy, Oct 23, 2003.

  1. Tim Clacy

    Tim Clacy Guest

    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
    Tim Clacy, Oct 23, 2003
    #1
    1. Advertising

  2. Tim Clacy

    Jerry Coffin Guest

    In article <3f97cf5e$0$268$>,
    amdk 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.
    Jerry Coffin, Oct 23, 2003
    #2
    1. Advertising

  3. Tim Clacy

    Tim Clacy Guest

    Jerry Coffin wrote:
    > In article <3f97cf5e$0$268$>,
    > amdk 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
    Tim Clacy, Oct 23, 2003
    #3
  4. Tim Clacy

    P.J. Plauger Guest

    "Tim Clacy" <> wrote in message
    news:3f97e6e5$0$257$...

    > 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
    P.J. Plauger, Oct 23, 2003
    #4
  5. Tim Clacy

    Tim Clacy Guest

    P.J. Plauger wrote:
    > "Tim Clacy" <> wrote in message
    > news:3f97e6e5$0$257$...
    >
    >> 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
    Tim Clacy, Oct 24, 2003
    #5
  6. Tim Clacy wrote in news:3f98ee84$0$262$:

    > P.J. Plauger wrote:
    >> "Tim Clacy" <> wrote in message
    >> news:3f97e6e5$0$257$...


    >> 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/
    Rob Williscroft, Oct 24, 2003
    #6
  7. Tim Clacy

    Tim Clacy Guest

    Rob Williscroft wrote:
    > Tim Clacy wrote in news:3f98ee84$0$262$:
    >
    >> P.J. Plauger wrote:
    >>> "Tim Clacy" <> wrote in message
    >>> news:3f97e6e5$0$257$...

    >
    >>> 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.
    Tim Clacy, Oct 24, 2003
    #7
  8. Tim Clacy

    lilburne Guest

    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
    lilburne, Oct 24, 2003
    #8
  9. Tim Clacy

    Default User Guest

    Tim Clacy wrote:

    > Blimey... Mr Plauger of Microsoft STL fame?



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




    Brian Rodenborn
    Default User, Oct 24, 2003
    #9
  10. Tim Clacy

    Ron Natalie Guest


    > 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.
    Ron Natalie, Oct 27, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,366
    Tony Morris
    Oct 19, 2004
  2. =?ISO-8859-1?Q?Bj=F8rn_Augestad?=

    Is it ok to define __STDC_VERSION__?

    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Jul 11, 2003, in forum: C Programming
    Replies:
    11
    Views:
    835
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=
    Jul 18, 2003
  3. bugbear
    Replies:
    4
    Views:
    425
    bugbear
    Aug 23, 2006
  4. Hicham Mouline
    Replies:
    5
    Views:
    2,352
    James Kanze
    Dec 19, 2008
  5. Francois Grieu
    Replies:
    0
    Views:
    294
    Francois Grieu
    Sep 12, 2012
Loading...

Share This Page