class static variables & __STDC_VERSION__

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Clacy

    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


  • Jerry Coffin

    #2
    Re: class static variables &amp; __STDC_VERSION_ _

    In article <3f97cf5e$0$268 $4d4eb98e@news. dk.uu.net>,
    nospamtcl@nospa mphaseone.nospamdk says...[color=blue]
    > 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'):[/color]

    [ ... ]
    [color=blue]
    > Can the standard pre-processor definition __STDC_VERSION_ _ be tested? If so,
    > for what value?[/color]

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

    Comment

    • Tim Clacy

      #3
      Re: class static variables &amp; __STDC_VERSION_ _

      Jerry Coffin wrote:[color=blue]
      > In article <3f97cf5e$0$268 $4d4eb98e@news. dk.uu.net>,
      > nospamtcl@nospa mphaseone.nospamdk says...[color=green]
      >> 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'):[/color]
      >
      > [ ... ]
      >[color=green]
      >> Can the standard pre-processor definition __STDC_VERSION_ _ be
      >> tested? If so, for what value?[/color]
      >
      > __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.[/color]

      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


      Comment

      • P.J. Plauger

        #4
        Re: class static variables &amp; __STDC_VERSION_ _

        "Tim Clacy" <nospamtcl@nosp amphaseone.nosp amdk> wrote in message
        news:3f97e6e5$0 $257$4d4eb98e@n ews.dk.uu.net.. .
        [color=blue]
        > I never knew __cplusplus had a value :-O So, this is about the best that can
        > be done then...
        >
        > #if __cplusplus >= 199711[/color]

        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.



        Comment

        • Tim Clacy

          #5
          Re: class static variables &amp; __STDC_VERSION_ _

          P.J. Plauger wrote:[color=blue]
          > "Tim Clacy" <nospamtcl@nosp amphaseone.nosp amdk> wrote in message
          > news:3f97e6e5$0 $257$4d4eb98e@n ews.dk.uu.net.. .
          >[color=green]
          >> I never knew __cplusplus had a value :-O So, this is about the best
          >> that can be done then...
          >>
          >> #if __cplusplus >= 199711[/color]
          >
          > 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[/color]

          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


          Comment

          • Rob Williscroft

            #6
            Re: class static variables &amp; __STDC_VERSION_ _

            Tim Clacy wrote in news:3f98ee84$0 $262$4d4eb98e@n ews.dk.uu.net:
            [color=blue]
            > P.J. Plauger wrote:[color=green]
            >> "Tim Clacy" <nospamtcl@nosp amphaseone.nosp amdk> wrote in message
            >> news:3f97e6e5$0 $257$4d4eb98e@n ews.dk.uu.net.. .[/color][/color]
            [color=blue][color=green]
            >> Some older implementations merely #define __cplusplus as an empty
            >> token sequence. For maximum portability, you might favor:
            >>
            >> #if __cplusplus + 0 >= 199711
            >>[/color][/color]
            [color=blue]
            > 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)?
            >[/color]

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

            Comment

            • Tim Clacy

              #7
              Re: class static variables &amp; __STDC_VERSION_ _

              Rob Williscroft wrote:[color=blue]
              > Tim Clacy wrote in news:3f98ee84$0 $262$4d4eb98e@n ews.dk.uu.net:
              >[color=green]
              >> P.J. Plauger wrote:[color=darkred]
              >>> "Tim Clacy" <nospamtcl@nosp amphaseone.nosp amdk> wrote in message
              >>> news:3f97e6e5$0 $257$4d4eb98e@n ews.dk.uu.net.. .[/color][/color]
              >[color=green][color=darkred]
              >>> Some older implementations merely #define __cplusplus as an empty
              >>> token sequence. For maximum portability, you might favor:
              >>>
              >>> #if __cplusplus + 0 >= 199711
              >>>[/color][/color]
              >[color=green]
              >> 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)?
              >>[/color]
              >
              > 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.[/color]

              Excellent; thanks.


              Comment

              • lilburne

                #8
                Re: class static variables &amp; __STDC_VERSION_ _

                Tim Clacy wrote:[color=blue][color=green]
                >>[/color]
                >
                > Blimey... Mr Plauger of Microsoft STL fame?[/color]

                That isn't very nice - there are sonmethings in a
                distibguished career that are best forgoten


                Comment

                • Default User

                  #9
                  Re: class static variables &amp; __STDC_VERSION_ _

                  Tim Clacy wrote:
                  [color=blue]
                  > Blimey... Mr Plauger of Microsoft STL fame?[/color]


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




                  Brian Rodenborn

                  Comment

                  • Ron Natalie

                    #10
                    Re: class static variables &amp; __STDC_VERSION_ _

                    [color=blue]
                    > Blimey... Mr Plauger of Microsoft STL fame?[/color]

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


                    Comment

                    Working...