🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.c
Article #309938

Re: Błąd preprocesora czy czegoś nie rozumiem?

#309938
From: M.M.
Date: Thu, 03 Oct 2019 22:40
65 lines
3406 bytes
On Friday, October 4, 2019 at 12:29:57 AM UTC+2, Maciek Godek wrote:
> 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__)

Spróbowałęm tego najpierw, ten sam efekt:


#define TEST1( __x__ ) int __x__  = __LINE__
#define TEST2_( __x__  , __l__ ) int __x__  ## __l__ = 0
#define TEST2( __x__ ) TEST2_(__x__  , __LINE__)

int main(int argc, char *argv[]) {
    TEST1( x );
    printf( "%d\n" , x );
    TEST2( y );
    TEST2( y );
    return 0;
}

test_01.cpp:17: błąd: redeclaration of 'int y__LINE__'
     TEST2( y );
            ^

Pozdrawiam
Technical Details
Message-ID: <ed39a49f-5fa2-4573-a688-08b160d397d4@googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!3.eu.feeder.erje.net!feeder.erje.net!weretis.net!feeder7.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!o24no4012709qtl.0!news-out.google.com!x7ni693qtf.0!nntp.google.com!o24no4012703qtl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
References: <780a7f39-337b-4b6b-810f-7c4151e10f74@googlegroups.com> <f1c8f951-ad5b-4fca-8e58-2fb7a4850aaa@googlegroups.com> <1abb73f5-4326-419c-a08d-499fbef10226@googlegroups.com>

Show Full Headers
Complete Article Headers
X-Received: by 2002:a37:b5c2:: with SMTP id e185mr7179048qkf.247.1570142421965;        Thu, 03 Oct 2019 15:40:21 -0700 (PDT)
X-Received: by 2002:a81:4c43:: with SMTP id z64mr8519394ywa.292.1570142421772; Thu, 03 Oct 2019 15:40:21 -0700 (PDT)
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!3.eu.feeder.erje.net!feeder.erje.net!weretis.net!feeder7.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!o24no4012709qtl.0!news-out.google.com!x7ni693qtf.0!nntp.google.com!o24no4012703qtl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: pl.comp.lang.c
Date: Thu, 3 Oct 2019 15:40:21 -0700 (PDT)
In-Reply-To: <1abb73f5-4326-419c-a08d-499fbef10226@googlegroups.com>
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=159.205.34.49; posting-account=xjvq9QoAAAATMPC2X3btlHd_LkaJo_rj
NNTP-Posting-Host: 159.205.34.49
References: <780a7f39-337b-4b6b-810f-7c4151e10f74@googlegroups.com> <f1c8f951-ad5b-4fca-8e58-2fb7a4850aaa@googlegroups.com> <1abb73f5-4326-419c-a08d-499fbef10226@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ed39a49f-5fa2-4573-a688-08b160d397d4@googlegroups.com>
Subject: Re: Błąd preprocesora czy
 czegoś nie rozumiem?
From: "M.M." <mmarszik@gmail.com>
Injection-Date: Thu, 03 Oct 2019 22:40:21 +0000
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit