Page MenuHomePhabricator

Allow constexpr construction of subobjects unconditionally, not just in C++14.
ClosedPublic

Authored by dlj on Jan 6 2017, 5:50 PM.

Details

Summary

Per https://wg21.link/CWG1677, the C++11 standard did not clarify that constant
initialization of an object allowed constexpr brace-or-equal initialization of
subobjects:

struct foo_t { union { int i; volatile int j; } u; };

__attribute__((__require_constant_initialization__))
static const foo_t x = {{0}};

Because foo_t::u has a volatile member, the initializer for x fails. However,
there is really no good reason, because this:

union foo_u { int i; volatile int j; };
__attribute__((__require_constant_initialization__))
static const foo_u x = {0};

does have a constant initializer.

(This was triggered by musl's pthread_mutex_t type when building under C++11.)

Event Timeline

dlj updated this revision to Diff 83476.Jan 6 2017, 5:50 PM
dlj retitled this revision from to Allow constexpr construction of subobjects unconditionally, not just in C++14..
dlj updated this object.
dlj added a reviewer: rsmith.
dlj added subscribers: cfe-commits, EricWF.
rsmith edited edge metadata.Jan 7 2017, 2:09 AM

Can you add a test please?

dlj updated this revision to Diff 83647.Jan 9 2017, 10:23 AM
dlj edited edge metadata.
  • Add a test, and fix codegen test (the array init is now constant, even though there is a dtor).
dlj added a comment.Jan 9 2017, 10:34 AM

Test added, and fixed another one that I missed before.

dlj updated this revision to Diff 83650.Jan 9 2017, 10:34 AM
  • Fix lit checks.
rsmith accepted this revision.Jan 9 2017, 1:29 PM
rsmith edited edge metadata.

LGTM

This revision is now accepted and ready to land.Jan 9 2017, 1:29 PM
This revision was automatically updated to reflect the committed changes.