-
Notifications
You must be signed in to change notification settings - Fork 71
Fix Variant #768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sfod
wants to merge
28
commits into
main
Choose a base branch
from
fix-variant
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Fix Variant #768
+725
−364
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue #, if available:
This PR resolves two issues with Crt::Variant.
1. Compilation error with move-only underlying types on Windows platform
When building shared libraries on Windows platform, we use
__declspec(dllexport)
declaration for classes. According to MS docs, "class templates are explicitly instantiated and the class's members must be defined".So, for the marked classes compiler will try to generate all special members (copy and move constructors and assignment operators; and default constructor if needed).
For example, take the following class:
The problem is that
Crt::Variant
has a user-defined special member functions. So, compiler sees that theCrt::Variant<Foo>
class has a certain special member defined, so compiler tries to instantiate (or generate) this special member.The issue appears when the Crt::Variant underlying class (
Foo
in the example above) is move-only, or copy-only, or non-default-constructible. For example, ifFoo
is move-only, compiler still sees a user-defined move constructor and move assignment operator inCrt::Variant
, decides that they can be used, and tries to instantiate them. And we see something like this:This issue could be resolved by explicitly deleting special members in user classes (
MyTest
in the example). However, this approach is not very convenient for Crt::Variant users.2. Compilation error on old gcc compilers when class's constructor has noexcept specifier
Constructors in
Crt::Variant
doesn't have noexcept specifier. When user class tries to usenoexcept
it causes compilation error on older gcc compilers:This stopped to be an error in gcc 10.
Description of changes:
The first problem can be resolved by removing user-defined special members from Crt::Variant. It can be achieved by moving all implementation details into
VariantImpl
, andVariant
members will just delegate all work toVariantImpl
.The special members in
Variant
will be either defaulted or deleted, depending on the underlying types traits. This can be achieved by inheriting from special auxiliary classes with defaulted/deleted special members.The simplified example can be found here: https://godbolt.org/z/Yz791jKoM
Add
noexcept
specifier to theCrt::Variant
default constructor if the first underlying type is nothrow-constructible.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.