Repository navigation
Fix constexpr constructors in dllimport classes [GH #583] - #584
jakub-labenski wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
AcknowlEDG review results updated for
|
|
AcknowlEDG review results updated for
CI success. |
daveedvdv-nvidia
left a comment
There was a problem hiding this comment.
Thanks for the fix and the tests.
Checking MSVC's behavior on Compiler Explorer (x64, 19.20 through 19.latest, /std:c++17 and /std:c++20), with
struct __declspec(dllimport) Y { // Explicitly constexpr constructor.
constexpr Y() noexcept = default;
virtual void f();
};
struct __declspec(dllimport) Z { // Implicit constructor.
virtual void f();
};
reveals:
| Microsoft mode | MSVC | EDG main |
This PR |
|---|---|---|---|
declaration of Y |
OK | error | OK |
constexpr Y y{}; |
OK (static init, local vftable) | (error on Y) |
OK |
static_assert((Y{}, true)); |
OK | (error on Y) |
OK |
constinit Y y; |
error C2127 | (error on Y) |
OK |
constexpr Z z{}; |
OK (static init, local vftable) | error | error |
static_assert((Z{}, true)); |
OK | error | error |
constinit Z z; |
error C2127 | error | error |
(MSVC emits a local copy of the vftable of a dllimport class, ??_7Y@@6B@ rather than __imp_..., so a constant-initialized object doesn't depend on the import.)
So MSVC doesn't distinguish between an explicitly-constexpr constructor and an implicit one here: In both cases the constructor is usable in constant expressions. The remaining Microsoft-mode restriction on the implicit constructor therefore doesn't match MSVC either (constexpr Z z{}; is accepted by MSVC). Could the restriction be removed entirely, rather than limited to implicit constructors? MSVC's rejection of constinit for both classes looks like an MSVC quirk (it uses dynamic initialization for non-constexpr variables of such classes), and may not be worth emulating at this time?
It would also be good for the tests to cover uses in constant expressions (a constexpr variable or a static_assert), not just the declarations.
Also, please add a //remark: line to the two new tests (makes it a little easier to review failing test results).
Summary
default_ctor_can_be_constexpr rejected default constructors of dllimport classes with virtual functions too broadly. It rejected explicitly constexpr defaulted constructors and also suppressed implicit constexpr constructors in GNU and Clang emulation modes.
This change accepts explicitly declared constexpr constructors when they meet the usual requirements. It limits the existing restriction on implicitly generated constructors to Microsoft mode.
Coverage
Adds regression tests for direct and inherited virtual functions, a virtual destructor, and a default member initializer, plus a pending Changes entry.
Verification
Related discussion: #583
Summary by CodeRabbit
constexprdefaulted constructors indllimportclasses with virtual functions when they meet the usualconstexprrequirements.dllimportprevent an implicitly declared default constructor from beingconstexprin classes with virtual functions.