-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Adjusting type_caster<std::reference_wrapper<T>>
to support const/non-const propagation in cast_op
.
#2705
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
Adjusting type_caster<std::reference_wrapper<T>>
to support const/non-const propagation in cast_op
.
#2705
Changes from 18 commits
65abb62
c603907
5cdc0ca
35e7fa5
4cd06d3
5abd823
c5bd104
f0e41f5
b56a568
d71cd7a
3c69635
8f32ba3
d144e59
a7ec72e
e1aca4b
005d3fe
4ac23ca
a3d9b18
5b4fb98
8dcbc42
9f77862
7672e92
c747ae2
33f57cc
d869976
f109edd
775a7c9
6f6dac7
fd6dc5b
b3ecc53
c22772b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -960,9 +960,15 @@ template <typename type> class type_caster<std::reference_wrapper<type>> { | |
private: | ||
using caster_t = make_caster<type>; | ||
caster_t subcaster; | ||
using subcaster_cast_op_type = typename caster_t::template cast_op_type<type>; | ||
static_assert(std::is_same<typename std::remove_const<type>::type &, subcaster_cast_op_type>::value, | ||
"std::reference_wrapper<T> caster requires T to have a caster with an `T &` operator"); | ||
using reference_t = typename std::add_lvalue_reference<type>::type; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be more specific, According to cppreference.com, this is where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
using subcaster_cast_op_type = | ||
laramiel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
typename caster_t::template cast_op_type<reference_t>; | ||
|
||
static_assert((std::is_same<typename std::remove_const<type>::type &, | ||
subcaster_cast_op_type>::value || | ||
std::is_same<reference_t, subcaster_cast_op_type>::value), | ||
"std::reference_wrapper<T> caster requires T to have a caster " | ||
"with an `T &` operator or `const T&` operator"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
public: | ||
bool load(handle src, bool convert) { return subcaster.load(src, convert); } | ||
static constexpr auto name = caster_t::name; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,7 @@ set(PYBIND11_TEST_FILES | |
test_constants_and_functions.cpp | ||
test_copy_move.cpp | ||
test_custom_type_casters.cpp | ||
test_chimera.cpp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This name stands out as the only one that is non-descriptive of what is being tested. The name is fitting if the context of the test is known first, but doesn't help people understand what the test is doing without looking inside. Could you please find a descriptive name, in line with the existing test names? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
test_docstring_options.cpp | ||
test_eigen.cpp | ||
test_enum.cpp | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -315,6 +315,7 @@ def test_reference_wrapper(): | |
"""std::reference_wrapper for builtin and user types""" | ||
assert m.refwrap_builtin(42) == 420 | ||
assert m.refwrap_usertype(UserType(42)) == 42 | ||
assert m.refwrap_usertype_const(UserType(42)) == 42 | ||
|
||
with pytest.raises(TypeError) as excinfo: | ||
m.refwrap_builtin(None) | ||
|
@@ -324,6 +325,15 @@ def test_reference_wrapper(): | |
m.refwrap_usertype(None) | ||
assert "incompatible function arguments" in str(excinfo.value) | ||
|
||
assert m.refwrap_lvalue().value == 1 | ||
m.refwrap_lvalue().value = 4 | ||
assert m.refwrap_lvalue().value == 4 | ||
|
||
# const-ness is not propagated. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At second thought, I think it's best to only exercise
and to delete the comment and the other two lines. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
assert m.refwrap_lvalue_const().value == 1 | ||
m.refwrap_lvalue_const().value = 2 | ||
assert m.refwrap_lvalue_const().value == 2 | ||
|
||
a1 = m.refwrap_list(copy=True) | ||
a2 = m.refwrap_list(copy=True) | ||
assert [x.value for x in a1] == [2, 3] | ||
|
Uh oh!
There was an error while loading. Please reload this page.