Skip to content

Commit eb0f1cc

Browse files
committed
Only allow unchecked()/mutable_unchecked() on an lvalue
This should mitigate accidental invocation on a temporary array. Fixes #961.
1 parent 7918bcc commit eb0f1cc

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

include/pybind11/numpy.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ class array : public buffer {
689689
* care: the array must not be destroyed or reshaped for the duration of the returned object,
690690
* and the caller must take care not to access invalid dimensions or dimension indices.
691691
*/
692-
template <typename T, ssize_t Dims = -1> detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() {
692+
template <typename T, ssize_t Dims = -1> detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() & {
693693
if (Dims >= 0 && ndim() != Dims)
694694
throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) +
695695
"; expected " + std::to_string(Dims));
@@ -703,7 +703,7 @@ class array : public buffer {
703703
* reshaped for the duration of the returned object, and the caller must take care not to access
704704
* invalid dimensions or dimension indices.
705705
*/
706-
template <typename T, ssize_t Dims = -1> detail::unchecked_reference<T, Dims> unchecked() const {
706+
template <typename T, ssize_t Dims = -1> detail::unchecked_reference<T, Dims> unchecked() const & {
707707
if (Dims >= 0 && ndim() != Dims)
708708
throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) +
709709
"; expected " + std::to_string(Dims));
@@ -876,7 +876,7 @@ template <typename T, int ExtraFlags = array::forcecast> class array_t : public
876876
* care: the array must not be destroyed or reshaped for the duration of the returned object,
877877
* and the caller must take care not to access invalid dimensions or dimension indices.
878878
*/
879-
template <ssize_t Dims = -1> detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() {
879+
template <ssize_t Dims = -1> detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() & {
880880
return array::mutable_unchecked<T, Dims>();
881881
}
882882

@@ -887,7 +887,7 @@ template <typename T, int ExtraFlags = array::forcecast> class array_t : public
887887
* for the duration of the returned object, and the caller must take care not to access invalid
888888
* dimensions or dimension indices.
889889
*/
890-
template <ssize_t Dims = -1> detail::unchecked_reference<T, Dims> unchecked() const {
890+
template <ssize_t Dims = -1> detail::unchecked_reference<T, Dims> unchecked() const & {
891891
return array::unchecked<T, Dims>();
892892
}
893893

0 commit comments

Comments
 (0)