From eb0f1cc7bfdb957d9e60f93ea91af9d56e3351a7 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Tue, 25 Jul 2017 16:22:45 -0400 Subject: [PATCH] Only allow unchecked()/mutable_unchecked() on an lvalue This should mitigate accidental invocation on a temporary array. Fixes #961. --- include/pybind11/numpy.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 388e21229..57d442141 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -689,7 +689,7 @@ public: * care: the array must not be destroyed or reshaped for the duration of the returned object, * and the caller must take care not to access invalid dimensions or dimension indices. */ - template detail::unchecked_mutable_reference mutable_unchecked() { + template detail::unchecked_mutable_reference mutable_unchecked() & { if (Dims >= 0 && ndim() != Dims) throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) + "; expected " + std::to_string(Dims)); @@ -703,7 +703,7 @@ public: * reshaped for the duration of the returned object, and the caller must take care not to access * invalid dimensions or dimension indices. */ - template detail::unchecked_reference unchecked() const { + template detail::unchecked_reference unchecked() const & { if (Dims >= 0 && ndim() != Dims) throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) + "; expected " + std::to_string(Dims)); @@ -876,7 +876,7 @@ public: * care: the array must not be destroyed or reshaped for the duration of the returned object, * and the caller must take care not to access invalid dimensions or dimension indices. */ - template detail::unchecked_mutable_reference mutable_unchecked() { + template detail::unchecked_mutable_reference mutable_unchecked() & { return array::mutable_unchecked(); } @@ -887,7 +887,7 @@ public: * for the duration of the returned object, and the caller must take care not to access invalid * dimensions or dimension indices. */ - template detail::unchecked_reference unchecked() const { + template detail::unchecked_reference unchecked() const & { return array::unchecked(); }