-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<type_traits>: Implement Layout-compatibility and Pointer-interconvertibility Traits #1575
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
Merged
Stephan T. Lavavej (StephanTLavavej)
merged 9 commits into
microsoft:main
from
MahmoudGSaleh:master
Jan 31, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c8120cd
Draft implementation of P0466R5 Layout-Compatibility And Pointer-Inte…
MahmoudGSaleh 33c80cd
Updates to PR based on feedback
MahmoudGSaleh 54ce30d
Test fixes for consistency
MahmoudGSaleh cfbb97f
Fix clang-format issues
MahmoudGSaleh 6a8e3e9
Fix BE test failures and cv-qualifiers tests
MahmoudGSaleh 84083f1
Fix ordering of tests in feature_test_macros
MahmoudGSaleh f49d233
Apply PR code review feedback
MahmoudGSaleh c115942
Apply fixes from feedback comments
MahmoudGSaleh 53a91b1
static_cast nullptr to use template argument deduction.
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
4 changes: 4 additions & 0 deletions
4
tests/std/tests/P0466R5_layout_compatibility_and_pointer_interconvertibility_traits/env.lst
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_latest_matrix.lst |
254 changes: 254 additions & 0 deletions
254
tests/std/tests/P0466R5_layout_compatibility_and_pointer_interconvertibility_traits/test.cpp
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| #include <assert.h> | ||
| #include <type_traits> | ||
|
|
||
|
MahmoudGSaleh marked this conversation as resolved.
|
||
| using namespace std; | ||
|
|
||
| #define ASSERT(...) assert((__VA_ARGS__)) | ||
|
|
||
| struct S { // Must be declared at namespace scope due to static data member | ||
| static int s1; | ||
| int v1; | ||
| int v2; | ||
| }; | ||
|
|
||
| constexpr bool test() { | ||
| #ifndef __EDG__ // TRANSITION, VSO-1268984 | ||
| #ifndef __clang__ // TRANSITION, LLVM-48860 | ||
| // is_layout_compatible tests | ||
| { | ||
| struct S0 { | ||
| int v1; | ||
| int v2; | ||
| }; | ||
|
|
||
| struct S1 { | ||
| S0 s1; | ||
| int v3; | ||
| }; | ||
|
|
||
| struct S2 { | ||
| S0 s1; | ||
| int v2; | ||
| }; | ||
|
|
||
| struct S3 { | ||
| S0 s1; | ||
| int v2; | ||
| int v3; | ||
| }; | ||
|
|
||
| struct S4 { | ||
| int v1; | ||
|
|
||
| private: | ||
| int v2; | ||
| }; | ||
|
|
||
| struct S5 { | ||
| int v1; | ||
|
|
||
| private: | ||
| int v2; | ||
| }; | ||
|
|
||
| enum E1 { e1, e2, e3, e4 }; | ||
| enum E2 : int { e5 }; | ||
| enum E3 : unsigned int { e6, e7, e8 }; | ||
| enum class E4 : unsigned int { no, yes }; | ||
| enum class E5 { zero, fortytwo = 42 }; | ||
|
|
||
| ASSERT(is_layout_compatible_v<int, int>); | ||
| ASSERT(is_layout_compatible_v<const void, void>); | ||
| ASSERT(is_layout_compatible_v<S1, volatile S2>); | ||
| ASSERT(is_layout_compatible_v<S1, S2>); | ||
| ASSERT(is_layout_compatible_v<S4, S4>); | ||
| ASSERT(is_layout_compatible_v<const volatile S4, S4>); | ||
| ASSERT(is_layout_compatible_v<E1, E2>); | ||
| ASSERT(is_layout_compatible_v<E3, E4>); | ||
| ASSERT(is_layout_compatible_v<E5, E1>); | ||
| ASSERT(is_layout_compatible_v<const E1, E2>); | ||
| ASSERT(is_layout_compatible_v<volatile E3, const E4>); | ||
| ASSERT(is_layout_compatible_v<int[], int[]>); | ||
| ASSERT(is_layout_compatible_v<int[3], int[3]>); | ||
|
|
||
| #if defined(__clang__) || defined(__EDG__) // TRANSITION, VSO-1269781 | ||
| ASSERT(is_layout_compatible_v<const int[], int[]>); | ||
| ASSERT(is_layout_compatible_v<const int[3], int[3]>); | ||
| ASSERT(is_layout_compatible_v<int[], volatile int[]>); | ||
| #endif // TRANSITION, VSO-1269781 | ||
|
|
||
| ASSERT(!is_layout_compatible_v<int, char>); | ||
| ASSERT(!is_layout_compatible_v<int, void>); | ||
| ASSERT(!is_layout_compatible_v<S1, void>); | ||
| ASSERT(!is_layout_compatible_v<S1, S3>); | ||
| ASSERT(!is_layout_compatible_v<S4, S5>); | ||
| ASSERT(!is_layout_compatible_v<E1, void>); | ||
| ASSERT(!is_layout_compatible_v<E1, E3>); | ||
| ASSERT(!is_layout_compatible_v<E2, E4>); | ||
| ASSERT(!is_layout_compatible_v<int[], int[2]>); | ||
| ASSERT(!is_layout_compatible_v<int[3], int[1]>); | ||
| } | ||
|
MahmoudGSaleh marked this conversation as resolved.
MahmoudGSaleh marked this conversation as resolved.
|
||
|
|
||
| // is_pointer_interconvertible_base_of tests | ||
| { | ||
| class A {}; | ||
| class B : public A {}; | ||
| class C : public A { | ||
| int : 0; | ||
| }; | ||
| class D : public C {}; | ||
| // Disable warning C4408: anonymous union did not declare any data members | ||
| #pragma warning(push) | ||
| #pragma warning(disable : 4408) | ||
| class E : public A { | ||
| union {}; | ||
| }; | ||
| #pragma warning(pop) | ||
| class F : private A {}; // Non-public inheritance | ||
| class NS : public B, public C {}; // Non-standard layout | ||
| class I; // Incomplete | ||
|
|
||
| union U { | ||
| int i; | ||
| char c; | ||
| }; | ||
|
|
||
| ASSERT(is_pointer_interconvertible_base_of_v<A, A>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<A, B>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<A, const B>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<A, C>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<A, volatile C>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<volatile A, const C>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<A, D>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<A, E>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<A, F>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<C, D>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<I, I>); | ||
| ASSERT(is_pointer_interconvertible_base_of_v<const I, I>); | ||
|
|
||
| ASSERT(!is_pointer_interconvertible_base_of_v<int, int>); | ||
| ASSERT(!is_pointer_interconvertible_base_of_v<void, void>); | ||
| ASSERT(!is_pointer_interconvertible_base_of_v<A, int>); | ||
| ASSERT(!is_pointer_interconvertible_base_of_v<B, C>); | ||
| ASSERT(!is_pointer_interconvertible_base_of_v<A, NS>); | ||
| ASSERT(!is_pointer_interconvertible_base_of_v<B, NS>); | ||
|
StephanTLavavej marked this conversation as resolved.
|
||
| ASSERT(!is_pointer_interconvertible_base_of_v<int, I>); | ||
| ASSERT(!is_pointer_interconvertible_base_of_v<U, U>); | ||
| ASSERT(!is_pointer_interconvertible_base_of_v<U, I>); | ||
| } | ||
|
MahmoudGSaleh marked this conversation as resolved.
MahmoudGSaleh marked this conversation as resolved.
|
||
|
|
||
| // is_corresponding_member tests | ||
| { | ||
| struct S1 { | ||
| int v1; | ||
| int v2; | ||
| }; | ||
|
|
||
| struct S2 { | ||
| int w1; | ||
| int w2; | ||
| }; | ||
|
|
||
| struct S3 { | ||
| int v1; | ||
| int v2; | ||
| int v3; | ||
| }; | ||
|
|
||
| struct S4 { | ||
| char v1; | ||
| int v2; | ||
| int v3; | ||
| }; | ||
|
|
||
| struct S5 { | ||
| int v1; | ||
| int v2; | ||
| void* v3; | ||
| }; | ||
|
|
||
| struct S6 { | ||
| int v1; | ||
| int v2; | ||
| double v3; | ||
| }; | ||
|
|
||
| struct S7 { | ||
| int f1() { | ||
| return 0; | ||
| } | ||
| }; | ||
|
|
||
| struct NS : S1, S2 {}; // Non-standard layout | ||
|
|
||
| ASSERT(is_corresponding_member(&S1::v1, &S::v1)); | ||
| ASSERT(is_corresponding_member(&S1::v2, &S::v2)); | ||
| ASSERT(is_corresponding_member(&S1::v1, &S1::v1)); | ||
| ASSERT(is_corresponding_member(&S1::v2, &S1::v2)); | ||
| ASSERT(is_corresponding_member(&S1::v1, &S2::w1)); | ||
| ASSERT(is_corresponding_member(&S1::v2, &S2::w2)); | ||
| ASSERT(is_corresponding_member(&S1::v1, &S3::v1)); | ||
| ASSERT(is_corresponding_member(&S1::v2, &S3::v2)); | ||
| ASSERT(is_corresponding_member(&S5::v1, &S6::v1)); | ||
| ASSERT(is_corresponding_member(&S5::v2, &S6::v2)); | ||
|
|
||
| ASSERT(!is_corresponding_member(&S1::v1, &S1::v2)); | ||
| ASSERT(!is_corresponding_member(&S1::v2, &S1::v1)); | ||
| ASSERT(!is_corresponding_member(&S1::v2, &S2::w1)); | ||
| ASSERT(!is_corresponding_member(&S1::v1, &S4::v1)); | ||
| ASSERT(!is_corresponding_member(&S1::v2, &S4::v2)); | ||
| ASSERT(!is_corresponding_member(&S3::v1, &S4::v1)); | ||
| ASSERT(!is_corresponding_member(&S3::v2, &S4::v2)); | ||
| ASSERT(!is_corresponding_member(&S5::v1, &S6::v2)); | ||
| ASSERT(!is_corresponding_member(&S5::v2, &S6::v1)); | ||
| ASSERT(!is_corresponding_member(&S5::v3, &S6::v3)); | ||
| ASSERT(!is_corresponding_member<NS, NS>(&NS::v1, &NS::w1)); | ||
| ASSERT(!is_corresponding_member(&S7::f1, &S7::f1)); | ||
| ASSERT(!is_corresponding_member(static_cast<int S1::*>(nullptr), static_cast<int S2::*>(nullptr))); | ||
| ASSERT(!is_corresponding_member(&S1::v1, static_cast<int S2::*>(nullptr))); | ||
| } | ||
|
MahmoudGSaleh marked this conversation as resolved.
MahmoudGSaleh marked this conversation as resolved.
|
||
|
|
||
| // is_pointer_interconvertible_with_class tests | ||
| { | ||
| struct A { | ||
| int a; | ||
| }; | ||
|
|
||
| struct B { | ||
| int b; | ||
| }; | ||
|
|
||
| struct C { | ||
| int f1() { | ||
| return 0; | ||
| } | ||
| }; | ||
|
|
||
| struct NS : A, B {}; // Non-standard layout | ||
|
|
||
| union U { | ||
| int v1; | ||
| char v2; | ||
| }; | ||
|
|
||
| ASSERT(is_pointer_interconvertible_with_class(&A::a)); | ||
| ASSERT(is_pointer_interconvertible_with_class(&NS::b)); | ||
| ASSERT(is_pointer_interconvertible_with_class(&U::v1)); | ||
| ASSERT(is_pointer_interconvertible_with_class(&U::v2)); | ||
|
MahmoudGSaleh marked this conversation as resolved.
|
||
|
|
||
| ASSERT(!is_pointer_interconvertible_with_class<NS>(&NS::a)); | ||
| ASSERT(!is_pointer_interconvertible_with_class<NS>(&NS::b)); | ||
| ASSERT(!is_pointer_interconvertible_with_class(&C::f1)); | ||
| ASSERT(!is_pointer_interconvertible_with_class(static_cast<int A::*>(nullptr))); | ||
| } | ||
|
MahmoudGSaleh marked this conversation as resolved.
|
||
| #endif // __clang__ | ||
| #endif // __EDG__ | ||
| return true; | ||
| } | ||
|
|
||
| int main() { | ||
| static_assert(test()); | ||
| test(); | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.