Index a #define string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    #46
    Re: Index a #define string

    Kevin Goodsell <usenet2.spamfr ee.fusion@never box.com> spoke thus:
    [color=blue]
    > 'Evil' is a technical term in C++ circles. It doesn't mean "not to be
    > used under any circumstances". This is in the FAQ.[/color]

    Indeed, and I'm sorry I didn't consider that FAQt (to make a pun, or
    something) before making my claim.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

    Comment

    • Christopher Benson-Manica

      #47
      Re: Index a #define string

      David Harmon <source@netcom. com> spoke thus:
      [color=blue]
      > Example?[/color]

      This is belated, and I'm sure you won't like it, but some time ago the
      following appeared on comp.lang.c:

      #define YEAR ((((__DATE__ [7] - '0') * 10 + (__DATE__ [8] - '0')) * 10 \
      + (__DATE__ [9] - '0')) * 10 + (__DATE__ [10] - '0'))

      #define MONTH (__DATE__ [2] == 'n' ? 1 \
      : __DATE__ [2] == 'b' ? 2 \
      : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 3 : 4) \
      : __DATE__ [2] == 'y' ? 5 \
      : __DATE__ [2] == 'n' ? 6 \
      : __DATE__ [2] == 'l' ? 7 \
      : __DATE__ [2] == 'g' ? 8 \
      : __DATE__ [2] == 'p' ? 9 \
      : __DATE__ [2] == 't' ? 10 \
      : __DATE__ [2] == 'v' ? 11 : 12)

      #define DAY ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 \
      + (__DATE__ [5] - '0'))

      #define DATE_AS_INT (((YEAR - 2000) * 12 + MONTH) * 31 + DAY)

      It's nothing if not ingenious :)

      --
      Christopher Benson-Manica | I *should* know what I'm talking about - if I
      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • Xenos

        #48
        Re: Index a #define string


        "Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
        news:c61229$31g $3@chessie.cirr .com...[color=blue]
        > David Harmon <source@netcom. com> spoke thus:
        > #define MONTH (__DATE__ [2] == 'n' ? 1 \
        > : __DATE__ [2] == 'b' ? 2 \
        > : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 3 : 4) \
        > : __DATE__ [2] == 'y' ? 5 \
        > : __DATE__ [2] == 'n' ? 6 \
        > : __DATE__ [2] == 'l' ? 7 \
        > : __DATE__ [2] == 'g' ? 8 \
        > : __DATE__ [2] == 'p' ? 9 \
        > : __DATE__ [2] == 't' ? 10 \
        > : __DATE__ [2] == 'v' ? 11 : 12)
        >[/color]
        how does this work for "january" and "june"?


        Comment

        • Default User

          #49
          Re: Index a #define string

          David Harmon wrote:
          [color=blue]
          > Oh, that is all sophistry. You are saying basically, that you cannot
          > use templates in the identical fashion that you use macros. Well, so
          > what? The point is that you can accomplish the reasonable purpose
          > formerly served by macros, with templates, consts, and other C++
          > constructs that offer the advantages of being integrated with the
          > language. That you cannot perform exactly the same gyrations on the way
          > to getting there is not a bug, it's a feature.[/color]

          Here's a question, not designed to be argumentative or contradictory but
          rather a genuine request for information. What about macros similar to
          assert(), that provide error-checking at a certain level of development
          and then are removed in their entirety (test included) at another? Is
          there another way to accomplish this without preprocessor constructs?



          Brian Rodenborn

          Comment

          • Xenos

            #50
            Re: Index a #define string


            "Default User" <first.last@boe ing.com.invalid > wrote in message
            news:40841032.2 910EF27@boeing. com.invalid...[color=blue]
            > David Harmon wrote:
            >
            > Here's a question, not designed to be argumentative or contradictory but
            > rather a genuine request for information. What about macros similar to
            > assert(), that provide error-checking at a certain level of development
            > and then are removed in their entirety (test included) at another? Is
            > there another way to accomplish this without preprocessor constructs?
            >
            >
            >
            > Brian Rodenborn[/color]

            I believe this can be done with specialized templates. I think the BOOST
            library has an example of this.



            Comment

            • Ioannis Vranos

              #51
              Re: Index a #define string

              "Default User" <first.last@boe ing.com.invalid > wrote in message
              news:40841032.2 910EF27@boeing. com.invalid...[color=blue]
              >
              > Here's a question, not designed to be argumentative or contradictory but
              > rather a genuine request for information. What about macros similar to
              > assert(), that provide error-checking at a certain level of development
              > and then are removed in their entirety (test included) at another? Is
              > there another way to accomplish this without preprocessor constructs?[/color]


              I do not think there can be an alternative wth the same properties, but we
              are not talking about banning the macro mechanism from the language, nor not
              using the macro standard library facilities. But to avoid creating macros,
              when possible.






              Ioannis Vranos

              Comment

              • Christopher Benson-Manica

                #52
                Re: Index a #define string

                Xenos <dont.spam.me@s pamhate.com> spoke thus:
                [color=blue][color=green]
                >> #define MONTH (__DATE__ [2] == 'n' ? 1 \
                >> : __DATE__ [2] == 'b' ? 2 \
                >> : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 3 : 4) \
                >> : __DATE__ [2] == 'y' ? 5 \
                >> : __DATE__ [2] == 'n' ? 6 \
                >> : __DATE__ [2] == 'l' ? 7 \
                >> : __DATE__ [2] == 'g' ? 8 \
                >> : __DATE__ [2] == 'p' ? 9 \
                >> : __DATE__ [2] == 't' ? 10 \
                >> : __DATE__ [2] == 'v' ? 11 : 12)
                >>[/color]
                > how does this work for "january" and "june"?[/color]

                It doesn't, does it? I'm not sure whether it was the poster's fault
                or my failure to copy it correctly, but I can fix it:

                #define MONTH (__DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? 1 : 6) \
                : __DATE__ [2] == 'b' ? 2 \
                : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 3 : 4) \
                : __DATE__ [2] == 'y' ? 5 \
                : __DATE__ [2] == 'l' ? 7 \
                : __DATE__ [2] == 'g' ? 8 \
                : __DATE__ [2] == 'p' ? 9 \
                : __DATE__ [2] == 't' ? 10 \
                : __DATE__ [2] == 'v' ? 11 : 12)

                Nice catch.

                --
                Christopher Benson-Manica | I *should* know what I'm talking about - if I
                ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                Comment

                • Default User

                  #53
                  Re: Index a #define string

                  Xenos wrote:[color=blue]
                  >
                  > "Default User" <first.last@boe ing.com.invalid > wrote in message
                  > news:40841032.2 910EF27@boeing. com.invalid...[color=green]
                  > > David Harmon wrote:
                  > >
                  > > Here's a question, not designed to be argumentative or contradictory but
                  > > rather a genuine request for information. What about macros similar to
                  > > assert(), that provide error-checking at a certain level of development
                  > > and then are removed in their entirety (test included) at another? Is
                  > > there another way to accomplish this without preprocessor constructs?[/color][/color]
                  [color=blue]
                  >
                  > I believe this can be done with specialized templates. I think the BOOST
                  > library has an example of this.[/color]


                  Could you be a bit more specific?



                  Brian Rodenborn

                  Comment

                  • David Harmon

                    #54
                    Re: Index a #define string

                    On Sun, 18 Apr 2004 15:18:50 -0700 in comp.lang.c++, "Paul Mensonides"
                    <leavings@comca st.net> wrote,[color=blue]
                    >David Harmon wrote:[color=green]
                    >> Lexical analyzer? Show me a C preprocessor implementation of anything
                    >> to compare with Joel de Guzman's Spirit parser template library.
                    >> http://spirit.sf.net[/color]
                    >
                    >You want to know what is really funny about that? Joel de Guzman uses the
                    >preprocessor in Spirit and Fusion for exactly the kind of stuff that I'm talking
                    >about.[/color]

                    Joel does not use the preprocessor to implement a lexical analyser or
                    anything of the sort. Or perhaps you are ready to stop beating around
                    the bush and actually say what you are talking about. Spirit and for
                    that matter the rest of Boost uses the preprocessor mainly:

                    1. As a hackish work-around for various broken and incomplete
                    implementations of C++, mainly of templates. Nobody would deny that if
                    your implementation is broken, the preprocessor may be the work-around
                    you are left holding.

                    2. Debugging, which makes sense as debugging is closely tied to
                    program source code and _not_ to what you are otherwise trying to
                    implement. This often takes the form of inserting __FILE__ __LINE__ or
                    a fragment from the source into a debug string using the # preprocessor
                    operator, with the fact that a macro is doing it being almost
                    incidental.

                    I do indeed approve of the assert() macro and similar in the context of
                    C++ debugging, despite its hazards. Of course people like Walter Bright
                    would disagree, and in his D language assert() is implemented as a
                    compiler facility since there is no preprocessor as such remaining.


                    Comment

                    • Paul Mensonides

                      #55
                      Re: Index a #define string

                      David Harmon wrote:[color=blue]
                      > On Sun, 18 Apr 2004 15:18:50 -0700 in comp.lang.c++, "Paul Mensonides"
                      > <leavings@comca st.net> wrote,[color=green]
                      >> David Harmon wrote:[color=darkred]
                      >>> Lexical analyzer? Show me a C preprocessor implementation of
                      >>> anything
                      >>> to compare with Joel de Guzman's Spirit parser template library.
                      >>> http://spirit.sf.net[/color]
                      >>
                      >> You want to know what is really funny about that? Joel de Guzman
                      >> uses the preprocessor in Spirit and Fusion for exactly the kind of
                      >> stuff that I'm talking about.[/color]
                      >
                      > Joel does not use the preprocessor to implement a lexical analyser or
                      > anything of the sort. Or perhaps you are ready to stop beating around
                      > the bush and actually say what you are talking about. Spirit and for
                      > that matter the rest of Boost uses the preprocessor mainly:[/color]

                      How about:

                      spirit/core/non_terminal/rule.hpp
                      spirit/core/non_terminal/impl/rule.ipp
                      spirit/dynamic/select.hpp
                      spirit/dynamic/impl/switch.ipp
                      spirit/fusion/sequence/make_tupe.hpp
                      spirit/fusion/sequence/tie.hpp
                      spirit/fusion/sequence/tuple20.hpp
                      spirit/fusion/sequence/tuple30.hpp
                      spirit/fusion/sequence/tuple40.hpp
                      spirit/fusion/sequence/tuple50.hpp
                      spirit/fusion/sequence/tuple_forward.h pp
                      spirit/fusion/sequence/detail/generate.hpp
                      spirit/fusion/sequence/detail/tuple10.hpp
                      spirit/fusion/sequence/detail/tuple_builder.h pp
                      spirit/fusion/sequence/detail/tuple_macro.hpp
                      spirit/utility/grammar_def.hpp

                      All of those files use preprocessor metaprogramming to generate code--and that
                      is just Spirit/Fusion. Other examples include Boost.Function, Boost.Lambda,
                      Boost.MPL, Boost.Python, Boost.Test, Boost.Type_Trai ts, and Boost.Variant.
                      Further, those aren't even counting things that use the preprocessor in
                      extremely simple ways to generate code.

                      As you say below, Boost uses the preprocessor to deal with broken compilers and
                      minimal debugging, but that is not even close to the whole story.

                      Despite your accusation, I haven't been beating around the bush. I have been
                      saying exactly what I mean: the preprocessor is used, for example, to *generate*
                      source code for the express purpose of minimizing maintenance points and
                      avoiding repetitive coding. This is beyond the scope of what templates or other
                      "language proper" mechanisms can do. Period.

                      Regards,
                      Paul Mensonides


                      Comment

                      • Michiel Salters

                        #56
                        Re: Index a #define string

                        Christopher Benson-Manica <ataru@nospam.c yberspace.org> wrote in message news:<c61229$31 g$3@chessie.cir r.com>...[color=blue]
                        > David Harmon <source@netcom. com> spoke thus:
                        >[color=green]
                        > > Example?[/color]
                        >
                        > This is belated, and I'm sure you won't like it, but some time ago the
                        > following appeared on comp.lang.c:
                        >
                        > #define YEAR ((((__DATE__ [7] - '0') * 10 + (__DATE__ [8] - '0')) * 10 \
                        > + (__DATE__ [9] - '0')) * 10 + (__DATE__ [10] - '0'))
                        >
                        > #define MONTH (__DATE__ [2] == 'n' ? 1 \
                        > : __DATE__ [2] == 'b' ? 2 \
                        > : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 3 : 4) \
                        > : __DATE__ [2] == 'y' ? 5 \
                        > : __DATE__ [2] == 'n' ? 6 \
                        > : __DATE__ [2] == 'l' ? 7 \
                        > : __DATE__ [2] == 'g' ? 8 \
                        > : __DATE__ [2] == 'p' ? 9 \
                        > : __DATE__ [2] == 't' ? 10 \
                        > : __DATE__ [2] == 'v' ? 11 : 12)
                        >
                        > #define DAY ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 \
                        > + (__DATE__ [5] - '0'))
                        >
                        > #define DATE_AS_INT (((YEAR - 2000) * 12 + MONTH) * 31 + DAY)
                        >
                        > It's nothing if not ingenious :)[/color]

                        With the MONTH fix incorporated, you can do even do useful things:
                        ---------------------------------------
                        #include <cassert>

                        #define UNIQUE_NAME( Y,M,D ) ReviewBefore##_ ##Y##_##M##_##D ##_##line
                        #define REVIEW_DATE( YYYY, MM, DD ) (((YYYY - 2000) * 12 + MM) * 31 +
                        DD)
                        #define COMPILE_DATE (((YEAR - 2000) * 12 + MONTH) * 31 +
                        DAY)

                        #define REVIEW_BEFORE( YYYY, MM, DD ) \
                        static const struct UNIQUE_NAME(YYY Y,MM,DD)##__LIN E__ {
                        UNIQUE_NAME(YYY Y,MM,DD)##__LIN E__() { \
                        assert( COMPILE_DATE < REVIEW_DATE(YYY Y, MM, DD) ); \
                        } } UNIQUE_NAME(YYY Y,MM,DD)##__LIN E__

                        #define REVIEW_BEFORE_S TMT( YYYY, MM, DD ) \
                        assert( COMPILE_DATE < REVIEW_DATE(YYY Y, MM, DD) )
                        ---------------------------------------
                        Example Usage:

                        REVIEW_BEFORE( 2005, 01, 01 ); // Form 1:global

                        int main(int argc, char** argv)
                        {
                        REVIEW_BEFORE_S TMT( 2004, 01, 01 ); // Form 2: in function
                        }

                        Now your code has an expiry date :)

                        Regards,
                        Michiel Salters

                        Comment

                        • Christopher Benson-Manica

                          #57
                          Re: Index a #define string

                          Michiel Salters <Michiel.Salter s@logicacmg.com > spoke thus:
                          [color=blue]
                          > int main(int argc, char** argv)
                          > {
                          > REVIEW_BEFORE_S TMT( 2004, 01, 01 ); // Form 2: in function
                          > }[/color]
                          [color=blue]
                          > Now your code has an expiry date :)[/color]

                          Cute! And ingenious! Although some might argue that such code should
                          expire immediately ;)

                          --
                          Christopher Benson-Manica | I *should* know what I'm talking about - if I
                          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                          Comment

                          • Julie

                            #58
                            Re: Index a #define string

                            David Harmon wrote:
                            [snip][color=blue]
                            > Joel does not use the preprocessor to implement a lexical analyser or
                            > anything of the sort. Or perhaps you are ready to stop beating around
                            > the bush and actually say what you are talking about. Spirit and for
                            > that matter the rest of Boost uses the preprocessor mainly:
                            >
                            > 1. As a hackish work-around for various broken and incomplete
                            > implementations of C++, mainly of templates. Nobody would deny that if
                            > your implementation is broken, the preprocessor may be the work-around
                            > you are left holding.
                            >
                            > 2. Debugging, which makes sense as debugging is closely tied to
                            > program source code and _not_ to what you are otherwise trying to
                            > implement. This often takes the form of inserting __FILE__ __LINE__ or
                            > a fragment from the source into a debug string using the # preprocessor
                            > operator, with the fact that a macro is doing it being almost
                            > incidental.
                            >
                            > I do indeed approve of the assert() macro and similar in the context of
                            > C++ debugging, despite its hazards. Of course people like Walter Bright
                            > would disagree, and in his D language assert() is implemented as a
                            > compiler facility since there is no preprocessor as such remaining.
                            > http://www.digitalmars.com/D/[/color]

                            Those all sound like legitimate and 'non-evil' uses of macros to me.

                            Earlier in this thread:

                            David Harmon wrote:[color=blue]
                            >
                            > On Fri, 16 Apr 2004 16:05:00 +0000 (UTC) in comp.lang.c++, Christopher
                            > Benson-Manica <ataru@nospam.c yberspace.org> wrote,[color=green]
                            > >Ioannis Vranos <ivr@guesswh.at .emails.ru> spoke thus:
                            > >[color=darkred]
                            > >> Avoid macros completely.[/color]
                            > >
                            > >Not all macros are evil.[/color]
                            >
                            > Example?[/color]

                            So I guess you answered your own question!

                            Comment

                            • David Harmon

                              #59
                              Re: Index a #define string

                              On Tue, 20 Apr 2004 03:56:40 -0700 in comp.lang.c++, "Paul Mensonides"
                              <leavings@comca st.net> wrote,
                              [color=blue]
                              >Despite your accusation, I haven't been beating around the bush.[/color]

                              Sure you are. After several back-and-forth you have not posted one
                              single #define to illustrate a single one of your claims. I go to the
                              files you list:
                              [color=blue]
                              >spirit/core/non_terminal/rule.hpp[/color]

                              Not a #define in sight except for
                              #if !defined(BOOST_ SPIRIT_RULE_HPP )
                              #define BOOST_SPIRIT_RU LE_HPP
                              [color=blue]
                              >spirit/core/non_terminal/impl/rule.ipp[/color]

                              Not a #define in sight except for
                              #if !defined(BOOST_ SPIRIT_RULE_IPP )
                              #define BOOST_SPIRIT_RU LE_IPP
                              [color=blue]
                              >spirit/dynamic/select.hpp[/color]

                              Not present in the version of Spirit I am using.
                              [color=blue]
                              >spirit/dynamic/impl/switch.ipp[/color]

                              Not present in the version of Spirit I am using.

                              I give up. It is like "Read the encyclopedia and you will see what I
                              mean."
                              [color=blue]
                              >I have been saying exactly what I mean: the preprocessor is used, for
                              > example, to *generate* source code for the express purpose of minimizing
                              > maintenance points and avoiding repetitive coding.[/color]

                              Total floating abstract nonsense. Source code is what comes out of your
                              keyboard; and it includes whatever preprocessor macros you write. If
                              you "generate" it, it wouldn't be *source*.

                              So, in trying to fathom what, if anything, you are talking about, I run
                              in to the following comment from Joel. Kinda sums it all up:


                              ///////////////////////////////////////////////////////////////////////////
                              //
                              // BOOST_SPIRIT_CO NTEXT_PARSE helper macro
                              //
                              // The original implementation uses a template class. However, we
                              // need to lessen the template instantiation depth to help inferior
                              // compilers that sometimes choke on deep template instantiations.
                              // The objective is to avoid code redundancy. A macro, in this case
                              // is an obvious solution. Sigh!
                              //
                              // WARNING: INTERNAL USE ONLY. NOT FOR PUBLIC CONSUMPTION.
                              //
                              ///////////////////////////////////////////////////////////////////////////

                              Comment

                              • David Harmon

                                #60
                                Re: Index a #define string

                                On Tue, 20 Apr 2004 08:37:59 -0700 in comp.lang.c++, Julie
                                <julie@nospam.c om> wrote,
                                [color=blue]
                                >So I guess you answered your own question![/color]

                                In a sense, yes. But the discussion is pretty pointless for me if I
                                have to supply all the answers. It doesn't get me any closer to
                                understanding what you or Christopher or Paul might have in mind.

                                C++ took a big step forward when it took a step away from the
                                preprocessor and toward compile-time metaprogramming integrated with the
                                language. We are not all the way there yet, and cannot yet abandon
                                macros completely in C++. Perhaps we should say that macros are a
                                "necessary evil."


                                Comment

                                Working...