Bug 60184 - g++ does not allow static members of named unions
Summary: g++ does not allow static members of named unions
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.3
: P3 normal
Target Milestone: 5.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2014-02-13 19:39 UTC by d0sboots
Modified: 2014-06-06 16:02 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.7.0, 4.9.0
Last reconfirmed: 2014-06-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description d0sboots 2014-02-13 19:39:43 UTC
cat > test.cc <<EOF
union Test {
  static constexpr int kConstant = 10;
};
EOF
g++ --std=c++0x -c -o test.o test.cc

produces:
test.cc:2:36: error: 'Test::kConstant' may not be static because it is a member of a union

However, this is correct according to my reading of the standard, as well as the folks here: http://stackoverflow.com/questions/15996333/does-c11-allow-non-anonymous-unions-to-contain-static-data-members
It is also allowed by clang.

9/5 : "A union is a class defined with the class-key union; it holds only one data member at a time"

Section 9.4, in general, describes static members. It places no restrictions on static members in union-type classes.

9.5/1: "In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time."
There is no reason to specifically call out "non-static data members" unless static data members can exist.

9.5/5: "The member-specification of an anonymous union shall only define non-static data members."
This only applies to anonymous unions. That this is specifically called out for anonymous unions implies that it *is* allowed in general.

gcc -v:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
Comment 1 Paolo Carlini 2014-06-06 10:10:12 UTC
Seems easy to fix.
Comment 2 paolo@gcc.gnu.org 2014-06-06 16:02:10 UTC
Author: paolo
Date: Fri Jun  6 16:01:37 2014
New Revision: 211318

URL: http://gcc.gnu.org/viewcvs?rev=211318&root=gcc&view=rev
Log:
/cp
2014-06-06  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/60184
	* class.c (check_field_decls): In C++11 mode do not reject
	static data members and reference-type members in unions.

/testsuite
2014-06-06  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/60184
	* g++.dg/cpp0x/constexpr-union6.C: New.
	* g++.dg/cpp0x/union6.C: Likewise.
	* g++.dg/init/ref14.C: Adjust.
	* g++.dg/init/union1.C: Likewise.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-union6.C
    trunk/gcc/testsuite/g++.dg/cpp0x/union6.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/class.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/init/ref14.C
    trunk/gcc/testsuite/g++.dg/init/union1.C
Comment 3 Paolo Carlini 2014-06-06 16:02:48 UTC
Fixed for 4.10.0.