Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Could you please discuss whether casting a size_t variable to unsigned int variable is the correct way to handle... Date: Mon, 08 Jun 2015 14:52:45 -0700 Organization: None to speak of Lines: 68 Message-ID: <87egllrhoy.fsf@kst-u.example.com> References: <87ioc3j29h.fsf@kst-u.example.com> <3cd2ac79-aeaa-4b7a-91d7-6fc63f592f72@googlegroups.com> <87a8xfi01c.fsf@kst-u.example.com> <876180gufr.fsf@kst-u.example.com> <5922caf7-6301-40f8-8f28-64c1346774e2@googlegroups.com> <87mw0aqce9.fsf@kst-u.example.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="9be37d943435275679235b07c7947eef"; logging-data="9541"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18S2BcMkos/h3e4sKB9hvzM" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:Mz6n2l4FADoYIgEs9KXh/Wvf3rA= sha1:V443UckXVDdvXlx2d8RQ60xqwQE= Xref: csiph.com comp.lang.c:63096 glen herrmannsfeldt writes: > Keith Thompson wrote: > (snip) >> Because this strictly conforming program: > >> #include >> #include >> int main(void) { >> #ifdef size_t >> puts("size_t is a macro"); >> #else >> puts("size_t is not a macro"); >> #endif >> { >> char *size_t = "Because, among other things, if size_t were " >> "a macro, this would be a syntax error"; >> } >> return 0; >> } > >> must print "size_t is not a macro". > > Maybe I am missing something (likely), but why can't size_t be > a macro that expands to a typedef name? That is, with the appropriate > macro expansion it wouldn't be a syntax error, but still would print > "size_t is a macro". Interesting question. If an implementation is permitted to, for example, have the following in typedef unsigned long size_t; #define size_t size_t then the above program is *not* strictly conforming, but I don't think the macro definition would affect anything that doesn't explicitly test whether size_t is defined as a macro. I have some fairly weak arguments that it's not permitted. N1570 7.1.2p1 says: The header declares a set of related functions, plus any necessary types and additional macros needed to facilitate their use. The macro is not *needed* to facilitate the use of the type. 7.1.2p5 says: Any definition of an object-like macro described in this clause shall expand to code that is fully protected by parentheses where necessary, so that it groups in an arbitrary expression as if it were a single identifier. This cannot apply to a macro that expands to a typedef name. 7.1.4p1 gives explicit permission to define functions as macros. No such permission is given for type names. In general, I'd like to say that implementations aren't permitted to define identifiers in ways not explicitly permitted by the standard, but I'm not sure I can justify it rigorously. -- 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"