Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: Global const objects missing from object file Date: Sun, 07 Nov 2021 05:08:47 -0800 Organization: A noiseless patient Spider Lines: 52 Message-ID: <86ilx4aza8.fsf@linuxsc.com> References: <861r44ez7k.fsf@linuxsc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="2dc722b95f3891b2f37c442b9fb0adbf"; logging-data="26363"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CX7zUqLHqv1qEy9j4/3AsunjQS7s3bdQ=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:1aF83uT17eI6dYW4M7pb+1kYSgI= sha1:+ewu6K9MPWaO+gDiYox6pE5GpPg= Xref: csiph.com comp.lang.c++:82260 scott@slp53.sl.home (Scott Lurndal) writes: > Tim Rentsch writes: > >> Paavo Helde writes: >> >>> In some sense the do not differ. The const globals in C++ are >>> primarily meant for replacing C #define. Each C #define is TU-specific >>> and gets fully preprocessed by the preprocessor, no other TU-s are >>> involved. >> >> It appears you have sidestepped the central question here. What a >> definition like 'const int foo = 7;' (without any mention anywhere >> of 'extern') does in C++ is, AFAICT, exactly the same as a similar >> definition with static, namely 'static const int foo = 7;'. > > Is that not dependent upon scope? For example: > > class x { > static const unsigned long FRED = 0xabcdef00ul; > > }; Yes, it's true that leaving off 'static' here means something significantly different. Note however, (a) neither of these forms is allowed in C so there is no question of incompatibility; (b) the 'static'-less form wasn't allowed until C++11; (c) only the 'static' form works for the purpose of being usable in constant expressions, so the question here is moot. > requires an additional declaration in a compilation unit, e.g. > > const unsigned long x::FRED; In my tests such a declaration was needed only if the address of the static member FRED was taken. (I confess I didn't even try to consult the C++ standard to see if that result is officially okay or is merely a consequence of undefined behavior.) > While > > namespace y { > const unsigned long FRED = 0xabcdef00ul; > }; > > doesn't require an additional declaration. AFAICT declarations inside namespaces behave the same way as declarations at file scope, that is, const-without-static behaves just the same way as const-with-static (and the same rules for declarations/definitions, etc). So I don't think this scenario is an exception to what I said earlier.