Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.albasani.net!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.std.c++ Subject: Re: A preprocessor feature we should have had 40 years ago Date: Sat, 25 May 2013 02:24:09 -0700 (PDT) Organization: None to speak of Lines: 54 Sender: std-cpp-request@vandevoorde.com Approved: stephen.clamage@oracle.com Message-ID: References: NNTP-Posting-Host: iq7bFRtVF7LNrCXFje72hJ3cRZH+3r5R/33K4GccIAU= Content-Type: text/plain; charset=us-ascii X-Trace: news.albasani.net 5Pj5jSZrsJOohFW44vZOIlCaRY0RyEwLJyfBg/CnbSPnGNjKYv+Kx2lJ1YZ3Jsybu3HYrTlHA2OJ85iHUqc9mA== X-Complaints-To: abuse@albasani.net NNTP-Posting-Date: Sat, 25 May 2013 09:24:18 +0000 (UTC) X-Mailer: Perl5 Mail::Internet v2.05 X-Submission-Address: std-cpp-submit@vandevoorde.com Cancel-Lock: sha1:21g15tdzb69eV2IaFe9kxjoO2Cw= X-Original-Date: Fri, 24 May 2013 10:04:40 -0700 Xref: csiph.com comp.std.c++:622 "Paul D. DeRocco" 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 > ... > #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 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 ]