sc...@slp53.sl.home (Scott Lurndal) writes:
> Tim Rentsch <
tr.1...@z991.linuxsc.com> writes:
>
>> Paavo Helde <
myfir...@osa.pri.ee> 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.