http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57977

            Bug ID: 57977
           Summary: zero-length const array in union prohibits default
                    copy xtor
           Product: gcc
           Version: 4.7.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: daniel.santos at pobox dot com

#include <iostream>

union a {
    struct {
        const char string[0];
    } b;
};


int main(int argc, char *argv[]) {
    std::cout << "size = " << sizeof(union a) << std::endl;
    return 0;
}

Produces:

bug.cpp:6:4: error: member ‘a::<anonymous struct> a::b’ with copy assignment
operator not allowed in union
bug.cpp:6:4: note: unrestricted unions only available with -std=c++11 or
-std=gnu++11


So the zero-length array is, of course, a gcc extension.  I believe it is a bug
because it is zero-sized and no code needs to be generated to copy union
a::b::string.

My justification for using this is to aid in const correctness. I actually use
this in C in a kernel module where I create and populate it once and then
access it several times, so I just cast it as non-const when I populate it.  I
prefer to use the exact same header files for kernel & userland where possible.

Reply via email to