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: Jens Schweikhardt Newsgroups: comp.std.c++ Subject: Re: A preprocessor feature we should have had 40 years ago Followup-To: comp.std.c Date: Sun, 9 Jun 2013 02:21:00 -0700 (PDT) Organization: unknown Lines: 59 Sender: std-cpp-request@vandevoorde.com Approved: stephen.clamage@oracle.com Message-ID: References: NNTP-Posting-Host: TS4E/baOOP5o/JqT+zFwpa8yef/mOoco1S0+qumvfp8= X-Trace: news.albasani.net EyzLhIZDIlNgMNNvnjY/KgiaJ1EqrA84syRe4iYI37ysibCmXJimiaQq13NBWRHAps1D8BAMizo0JaDjRSfutw== X-Complaints-To: abuse@albasani.net NNTP-Posting-Date: Sun, 9 Jun 2013 09:21:14 +0000 (UTC) X-Mailer: Perl5 Mail::Internet v2.05 X-Submission-Address: std-cpp-submit@vandevoorde.com Cancel-Lock: sha1:8RzPMj76bplX2jbAIScJr7+pD5w= X-Original-Date: 8 Jun 2013 20:12:34 GMT Xref: csiph.com comp.std.c++:637 In comp.std.c Paul D. DeRocco wrote: # # 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 # }; Because almost *any* language other than the preprocessor langue you have in mind is already *better* suited for this task. For the above, in vi I type :r!perl -e 'foreach(0..100){print "$_ * $_,\n"}' with the result portable to before 1970. Using awk makes this even POSIX, since both vi and awk are standardized. If you're a purist, you could even write, compile and run a short C program. # 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. Maybe I'm the exception, but I never felt the need for such complicated repetitions where the limits involve preprocessor symbols. How many can you come up with that would convince a committee to invest time? Do a survey on how much open source C code would benefit from it by reducing code size significantly. I doubt you get very far. "Followup-To:" trimmed to comp.std.c. Regards, Jens -- Jens Schweikhardt http://www.schweikhardt.net/ SIGSIG -- signature too long (core dumped) [ 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 ]