extern const initialized warns in C
Jonathan Wakely
jwakely.gcc@gmail.com
Mon Jan 22 15:19:00 GMT 2018
On 21 January 2018 at 07:12, Jay K wrote:
> extern const int foo = 123;
>
>
>
> Why does this warn?
> This is a valid portable form, with the same meaning
> across all compilers, and, importantly, portably
> to C and C++.
>
> I explicitly do not want to say:
>
> const int foo = 123
>
> because I want the code to be valid and have the same meaning
> in C and C++ (modulo name mangling).
>
> I end up with:
>
> // Workaround gcc warning.
> #ifdef __cplusplus
> #define EXTERN_CONST extern const
> #else
> #define EXTERN_CONST const
> #endif
>
>
> EXTERN_CONST int foo = 123;
>
> and having to explain it to people.
Why not simply:
extern const int foo;
const int foo = 123;
without the ugly macro?
Or, according to taste:
// Without an extern declaration, file-scope const variables are static:
extern const int foo;
const int foo = 123;
Or:
#ifdef __cplusplus
// Without an extern declaration, file-scope const variables are static:
extern const int foo;
#endif
const int foo = 123;
But your macro version is just ugly. And instead of complaining that
you have to explain it to people, why not explain it with a comment in
the code? That's what comments are for.
More information about the Gcc
mailing list