Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #622

Re: A preprocessor feature we should have had 40 years ago

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.std.c++
Subject Re: A preprocessor feature we should have had 40 years ago
Date 2013-05-25 02:24 -0700
Organization None to speak of
Message-ID <lnzjvk2tsn.fsf@nuthaus.mib.org> (permalink)
References <UrKdnTRjb8pONQDMnZ2dnUVZ_oudnZ2d@earthlink.com>

Show all headers | View raw


"Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
> This is going to the C and C++ lists because it is equally applicable to
> both.
>
> I've occasionally needed to use the preprocessor to repeat a piece of
> code some variable number of times. What would be so hard about
> implementing this?
>
>      #repeat <identifier> <const-expression>
>      ...
>      #endrepeat
>
> The expression would be evaluated, and the contents of the block would
> be repeated that number of times, with the preprocessor identifier
> taking values 0 through n-1. Even with a fixed count, this can be useful:
>
>      const int squares[] = {
>      #repeat I 101
>          I * I,
>      #endrepeat
>      };
>
> Less trivial usages would of course involve counts that are themselves
> preprocessor symbols or expressions. The only way to do this now is to
> nest a lot of #if statements, up to some maximum size one might need,
> and explicitly write the contents of each iteration.

No, that's not the *only* way to do that.  You could write another
program that generates C source code from an input file in some well
defined format.  That format could even be standard C source with the
addition of a "#repeat" directive recognized by the program.  Make it
part of your build process, and voila, you have a #repeat directive.

In my opinion, the need to have simply repeated lines of code like
that is rare enough that it doesn't justify adding a new feature
to the preprocessor and to the language standard.

I wouldn't have *opposed* adding such a feature to the preprocessor
from the beginning, but I think adding it now would cause more
trouble than it's really worth, as new code that depends on it would
fail to compile on pre-C20XY compilers.  (Which is admittedly an
argument against adding *anything* to the language.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
     -- Antony Jay and Jonathan Lynn, "Yes Minister"


[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

A preprocessor feature we should have had 40 years ago "Paul D. DeRocco" <pderocco@ix.netcom.com> - 2013-05-23 19:18 -0600
  Re: A preprocessor feature we should have had 40 years ago Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-05-24 15:19 -0700
  Re: A preprocessor feature we should have had 40 years ago jacob navia <jacob@spamsink.net> - 2013-05-25 02:23 -0700
  Re: A preprocessor feature we should have had 40 years ago Keith Thompson <kst-u@mib.org> - 2013-05-25 02:24 -0700
    Re: A preprocessor feature we should have had 40 years ago "Kenneth \"Bessarion\" Boyd" <zaimoni@zaimoni.com> - 2013-05-25 23:43 -0700
    Re: A preprocessor feature we should have had 40 years ago "Paul D. DeRocco" <pderocco@ix.netcom.com> - 2013-05-25 23:44 -0700
      Re: A preprocessor feature we should have had 40 years ago Francis Glassborow <francis.glassborow@btinternet.com> - 2013-05-26 08:52 -0600
  Re: A preprocessor feature we should have had 40 years ago Rui Maciel <rui.maciel@googlemail.com> - 2013-05-25 23:43 -0700
  Re: A preprocessor feature we should have had 40 years ago Peter <pcurran88@googlemail.com> - 2013-05-30 19:40 -0600
    Re: A preprocessor feature we should have had 40 years ago "Paul D. DeRocco" <pderocco@ix.netcom.com> - 2013-06-01 02:04 -0600
      Re: A preprocessor feature we should have had 40 years ago ootiib@hot.ee - 2013-06-01 14:53 -0600
      Re: A preprocessor feature we should have had 40 years ago James Kuyper <jameskuyper@verizon.net> - 2013-06-04 10:41 -0600
        Re: A preprocessor feature we should have had 40 years ago espie@lain.home (Marc Espie) - 2013-06-05 23:12 -0700
          Re: A preprocessor feature we should have had 40 years ago James Kuyper <jameskuyper@verizon.net> - 2013-06-06 09:20 -0700
  Re: A preprocessor feature we should have had 40 years ago Jens Schweikhardt <usenet@schweikhardt.net> - 2013-06-09 02:21 -0700

csiph-web