[cfe-dev] static const POD object

Dmitri Gribenko gribozavr at gmail.com
Mon Feb 18 05:09:01 PST 2013


On Mon, Feb 18, 2013 at 12:47 PM, zapadinsky <zapadinsky at gmail.com> wrote:
> MSVC allows static const objects.
> In MSVC this code:
>
> struct A {
>         int v;
> };
> static const a;
>
> will compiles and a.v will be zero. But clang and gcc issue errors:
> error: default initialization of an object of const type 'const struct A'
> requires a user-provided default constructor
> error: uninitialized const ‘a’ [-fpermissive]
>
> There was resolved issue here:
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#78 , now
> standard states:
>
> [2003: 8.5/6]: Every object of static storage duration shall be
> zero-initialized at program startup before any other initialization takes
> place.
>
> [2003: 8.5/9]: If no initializer is specified for an object, and the object
> is of (possibly cv-qualified) non-POD class type (or array thereof), the
> object shall be default-initialized; if the object is of const-qualified
> type, the underlying class type shall have a user-declared default
> constructor. Otherwise, if no initializer is specified for a nonstatic
> object, the object and its subobjects, if any, have an indeterminate initial
> value; if the object or any of its subobjects are of const-qualified type,
> the program is ill-formed.
>
>
> Looks like Judy Ward suggestion was misunderstood.
>
> It's perfectly fine to instantiate a const static object according to this
> paragraphs. And this object should be zero-initialized constant object.

I don't think so.

> [...] if the object is of const-qualified
> type, the underlying class type shall have a user-declared default
> constructor. [...]

This requires a user-declared default constructor in this case.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/




More information about the cfe-dev mailing list