-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Test: Default Alias in Base Class #4581
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
ax3l
wants to merge
6
commits into
pybind:master
Choose a base branch
from
ax3l:topic-template-alias-base
base: master
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
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e009a87
Test: Default Alias in Base Class
ax3l e7f58da
Patch: `internals.h` for `same_type`
ax3l 073a76b
More Defensive `libstdc++` Test
ax3l af929fe
style: pre-commit fixes
pre-commit-ci[bot] 9d1d0bd
Always use custom hash
ax3l 4f57672
printf debugging of caster
ax3l 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "pybind11_tests.h" | ||
|
||
#include <memory> | ||
|
||
template <class T> | ||
using DefaultAllocator = std::allocator<T>; | ||
|
||
template <template <class> class Allocator = DefaultAllocator> | ||
struct Base {}; | ||
|
||
template <template <class> class Allocator = DefaultAllocator> | ||
struct S : public Base<Allocator> {}; | ||
|
||
// this returns S<DefaultAllocator> even though we register | ||
// the type S<std::allocator> | ||
// MSVC and Clang on Windows failed here to detect the registered type | ||
S<> make_S() { return S<>{}; } | ||
|
||
TEST_SUBMODULE(template_alias_base, m) { | ||
py::class_<Base<std::allocator>>(m, "B_std").def(py::init()); | ||
py::class_<S<std::allocator>, Base<std::allocator>>(m, "S_std").def(py::init()); | ||
|
||
m.def("make_S", &make_S); | ||
} |
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,11 @@ | ||
from pybind11_tests import template_alias_base as m | ||
|
||
|
||
def test_can_create_variable(): | ||
v = m.S_std() | ||
print(v) | ||
|
||
|
||
def test_can_return_variable(): | ||
v = m.make_S() | ||
print(v) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is basically stress-testing the compiler+library: can it figure out that
S<DefaultAllocator>
andS<std::allocator>
are the same?Question 1: Is it difficult to not have that stress-test baked into your downstream project? Either, by consistently avoiding the alias, or consistently using the alias.
Question 2: What happens if you patch internals.h similar to #4319?
If you decide to experiment more here, could you please not force push? That makes it more difficult for me to follow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct.
This is somewhat tricky in my case, especially across library/module interfaces. By consistently using one or the other, one has to hope a user does never use the other possible option of the alias.
Will try and push here.
Will do! I just force-pushed initially yesterday when I realized the initial test draft was not yet complete/buggy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed patch :)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rwgk I don't understand your patch yet:
The comment says, GNU's
libstdc++
is good (#if defined(__GLIBCXX__)
) - all other implementations need the self-made hash.Your patch says LLVM's
libc++
is not good (#if !defined(_LIBCPP_VERSION)
) - but all other implementations can use the standard hashes.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes: the CI for #4319 that ran yesterday (all successful) supports "LLVM's libc++ is not good (
#if !defined(_LIBCPP_VERSION)
) - but all other implementations can use the standard hashes."But it looks like your added test doesn't work universally any way you tried, is that a correct summary?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
My test works on Linux & Windows with libstdc++.