Commit 56784c4f by Yannick Jadoul Committed by GitHub

Add unchecked_reference::operator() and operator[] to overload resolution of…

Add unchecked_reference::operator() and operator[] to overload resolution of unchecked_mutable_reference (#2514)
parent 2b6b98e2
...@@ -419,6 +419,10 @@ class unchecked_mutable_reference : public unchecked_reference<T, Dims> { ...@@ -419,6 +419,10 @@ class unchecked_mutable_reference : public unchecked_reference<T, Dims> {
using ConstBase::ConstBase; using ConstBase::ConstBase;
using ConstBase::Dynamic; using ConstBase::Dynamic;
public: public:
// Bring in const-qualified versions from base class
using ConstBase::operator();
using ConstBase::operator[];
/// Mutable, unchecked access to data at the given indices. /// Mutable, unchecked access to data at the given indices.
template <typename... Ix> T& operator()(Ix... index) { template <typename... Ix> T& operator()(Ix... index) {
static_assert(ssize_t{sizeof...(Ix)} == Dims || Dynamic, static_assert(ssize_t{sizeof...(Ix)} == Dims || Dynamic,
......
...@@ -318,6 +318,18 @@ TEST_SUBMODULE(numpy_array, sm) { ...@@ -318,6 +318,18 @@ TEST_SUBMODULE(numpy_array, sm) {
return auxiliaries(r, r2); return auxiliaries(r, r2);
}); });
sm.def("proxy_auxiliaries1_const_ref", [](py::array_t<double> a) {
const auto &r = a.unchecked<1>();
const auto &r2 = a.mutable_unchecked<1>();
return r(0) == r2(0) && r[0] == r2[0];
});
sm.def("proxy_auxiliaries2_const_ref", [](py::array_t<double> a) {
const auto &r = a.unchecked<2>();
const auto &r2 = a.mutable_unchecked<2>();
return r(0, 0) == r2(0, 0);
});
// test_array_unchecked_dyn_dims // test_array_unchecked_dyn_dims
// Same as the above, but without a compile-time dimensions specification: // Same as the above, but without a compile-time dimensions specification:
sm.def("proxy_add2_dyn", [](py::array_t<double> a, double v) { sm.def("proxy_add2_dyn", [](py::array_t<double> a, double v) {
......
...@@ -364,6 +364,9 @@ def test_array_unchecked_fixed_dims(msg): ...@@ -364,6 +364,9 @@ def test_array_unchecked_fixed_dims(msg):
assert m.proxy_auxiliaries2(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32] assert m.proxy_auxiliaries2(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
assert m.proxy_auxiliaries2(z1) == m.array_auxiliaries2(z1) assert m.proxy_auxiliaries2(z1) == m.array_auxiliaries2(z1)
assert m.proxy_auxiliaries1_const_ref(z1[0, :])
assert m.proxy_auxiliaries2_const_ref(z1)
def test_array_unchecked_dyn_dims(msg): def test_array_unchecked_dyn_dims(msg):
z1 = np.array([[1, 2], [3, 4]], dtype='float64') z1 = np.array([[1, 2], [3, 4]], dtype='float64')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment