Article View: pl.comp.lang.c
Article #309937Re: Błąd preprocesora czy czegoś nie rozumiem?
From: Maciek Godek
Date: Thu, 03 Oct 2019 22:29
Date: Thu, 03 Oct 2019 22:29
43 lines
2873 bytes
2873 bytes
W dniu piątek, 4 października 2019 00:20:56 UTC+2 użytkownik Maciek Godek napisał:
> W dniu czwartek, 3 października 2019 22:19:36 UTC+2 użytkownik M.M. napisał:
> > Witam
> >
> > Jest sobie poniższy program i działa:
> >
> > #define TEST1( __x__ ) int __x__ = __LINE__
> > #define TEST2( __x__ ) int __x__ ## __LINE__ = 0
> >
> > int main(int argc, char *argv[]) {
> > TEST1( x );
> > printf( "%d\n" , x );
> > // TEST2( y );
> > // TEST2( y );
> > return 0;
> > }
> >
> > Jednak jak odkomentuję dwie line:
> > // TEST2( y );
> > // TEST2( y );
> >
> > to już mam błąd:
> >
> > redeclaration of 'int y__LINE__'
> > TEST2( y );
> > ^
> >
> > Od czego zależy kiedy rozwija makro __LINE__ a kiedy nie?
> >
> > Pozdrawiam
>
> Zdaje się, że kolejność, w jakiej preprocesor rozwija argumenty, jest niezdefiniowana w standardzie, i zależy od implementacji.
>
> Możesz spróbować taki trick:
>
> #define _TEST2_(x, line) int x##line = 0
> #define TEST2(x) _TEST2_(x, __LINE__)
a gdyby nie działało, spróbuj wprowadzić jeszcze jeden dodatkowy stopień zapośredniczenia, tj.
#define __TEST2__(x,line) int x##line = 0
#define _TEST2_(x,line) __TEST2__(x,line)
#define TEST2(x) _TEST2_(x,__LINE__)
Technical Details
Message-ID:
<1abb73f5-4326-419c-a08d-499fbef10226@googlegroups.com>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!2.eu.feeder.erje.net!4.us.feeder.erje.net!feeder.erje.net!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!o24no3986129qtl.0!news-out.google.com!x7ni693qtf.0!nntp.google.com!o24no3986126qtl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mailReferences:
<780a7f39-337b-4b6b-810f-7c4151e10f74@googlegroups.com> <f1c8f951-ad5b-4fca-8e58-2fb7a4850aaa@googlegroups.com>Show Full Headers
Complete Article Headers
X-Received: by 2002:a37:bc82:: with SMTP id m124mr7244617qkf.231.1570141796332; Thu, 03 Oct 2019 15:29:56 -0700 (PDT) X-Received: by 2002:a81:32d5:: with SMTP id y204mr8907095ywy.395.1570141796148; Thu, 03 Oct 2019 15:29:56 -0700 (PDT) Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!2.eu.feeder.erje.net!4.us.feeder.erje.net!feeder.erje.net!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!o24no3986129qtl.0!news-out.google.com!x7ni693qtf.0!nntp.google.com!o24no3986126qtl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: pl.comp.lang.c Date: Thu, 3 Oct 2019 15:29:55 -0700 (PDT) In-Reply-To: <f1c8f951-ad5b-4fca-8e58-2fb7a4850aaa@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=83.25.250.157; posting-account=f7iIKQoAAAAkDKpUafc-4IXhmRAzdB5r NNTP-Posting-Host: 83.25.250.157 References: <780a7f39-337b-4b6b-810f-7c4151e10f74@googlegroups.com> <f1c8f951-ad5b-4fca-8e58-2fb7a4850aaa@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <1abb73f5-4326-419c-a08d-499fbef10226@googlegroups.com> Subject: Re: Błąd preprocesora czy czegoś nie rozumiem? From: Maciek Godek <godek.maciek@gmail.com> Injection-Date: Thu, 03 Oct 2019 22:29:56 +0000 Lines: 47 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit