Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7615
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++, comp.lang.c |
| Subject | Re: Variable name that contains line number |
| Date | 2011-07-07 18:21 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <iv5bd3$tp8$1@dont-email.me> (permalink) |
| References | <c31ab4fb-ba3d-4544-a793-7583e3e04585@a31g2000vbt.googlegroups.com> <27e70ea8-5249-4d87-acb7-a9ec85accbb7@v12g2000vby.googlegroups.com> <c0a5a530-b1da-4b91-98fb-9bdb07ff2221@g12g2000yqd.googlegroups.com> |
Cross-posted to 2 groups.
On 7/6/2011 17:27, Paul N wrote:
> On Jul 6, 12:06 pm, Michael DOUBEZ<michael.dou...@free.fr> wrote:
>>
>> #define PCAT( x, y) x##y
>> #define SET_VAR_NNN(n) int PCAT(var,n)
>> #define SET_VAR SET_VAR_NNN(__LINE__)
>
> When I saw this question, I suspected the answer would be along those
> lines, but I'm afraid I still can't see why the second form works and
> the first form doesn't. Could someone explain to me?
>
> In my naive way, I would expect the preprocessor to either substitute
> the arguments and then the functions, or vice versa. So I would have
> expected either the first version to work as follows:
>
> SET_VAR; becomes
> SET_VAR_NNN(__LINE__); becomes
> SET_VAR_NNN(6); becomes
> var##6; becomes
> var6;
>
> (ie first version works)
>
> or for the second version to go:
>
> SET_VAR; becomes
> SET_VAR_NNN(__LINE__); becomes
> int PCAT(var,__LINE__); becomes
> int var##__LINE__; becomes
> int var__LINE__;
>
> (ie second version doesn't work).
>
> I can't see what magic it must do to allow the frst form not to work
> but the second to work. I've had a quick look at both the Standard and
> the FAQ, but the penny hasn't dropped.
The relevant portions of the Standard might be 6.10.3.3p2, 6.10.3.3p3,
6.10.3.4, if I understand them correctly.
SET_VAR
/* Replacement list */
SET_VAR_NNN(__LINE__)
/* Rescan finds 'SET_VAR_NNN()' and substitutes replacement list */
int PCAT(var,n)
/* Argument substitution "all macros contained therein" */
__LINE__
6
/* Argument substitution "after" */
int PCAT(var,6)
/* Rescan finds 'PCAT()' and substitutes replacement list*/
x##y
/* "...preceded or followed by a ##..." */
var6
/* Rescan finds nothing */
Contrast with the original:
SET_VAR
/* Replacement list */
SET_VAR_NNN(__LINE__)
/* Rescan finds 'SET_VAR_NNN()' and substitutes replacement list */
int var##n
/* "...preceded or followed by a ##..." */
int var__LINE__
/* Rescan finds nothing */
I hope this helps (and hope it doesn't contain an error)! :)
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
Variable name that contains line number Alex Vinokur <alex.vinokur@gmail.com> - 2011-07-06 03:29 -0700
Re: Variable name that contains line number Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-06 04:06 -0700
Re: Variable name that contains line number Alex Vinokur <alex.vinokur@gmail.com> - 2011-07-06 04:17 -0700
Re: Variable name that contains line number Puppet_Sock <puppet_sock@hotmail.com> - 2011-07-06 06:40 -0700
Re: Variable name that contains line number Juha Nieminen <nospam@thanks.invalid> - 2011-07-06 15:26 +0000
Re: Variable name that contains line number "Balog Pal" <pasa@lib.hu> - 2011-07-07 01:17 +0200
Re: Variable name that contains line number Paul N <gw7rib@aol.com> - 2011-07-06 14:27 -0700
Re: Variable name that contains line number Alexander Bartolich <alexander.bartolich@gmx.at> - 2011-07-07 01:50 +0000
Re: Variable name that contains line number Shao Miller <sha0.miller@gmail.com> - 2011-07-07 18:21 -0400
csiph-web