@@ -898,6 +898,18 @@ namespace xt
898898 (void ) size;
899899 XTENSOR_ASSERT_MSG (c.size () == size, " Trying to resize const data container with wrong size." );
900900 }
901+
902+ template <class S , class T >
903+ constexpr bool check_resize_dimension (const S&, const T&)
904+ {
905+ return true ;
906+ }
907+
908+ template <class T , size_t N, class S >
909+ constexpr bool check_resize_dimension (const std::array<T, N>&, const S& s)
910+ {
911+ return N == s.size ();
912+ }
901913 }
902914
903915 /* *
@@ -911,6 +923,8 @@ namespace xt
911923 template <class S >
912924 inline void xstrided_container<D>::resize(S&& shape, bool force)
913925 {
926+ XTENSOR_ASSERT_MSG (detail::check_resize_dimension (m_shape, shape),
927+ " cannot change the number of dimensions of xtensor" )
914928 std::size_t dim = shape.size ();
915929 if (m_shape.size () != dim || !std::equal (std::begin (shape), std::end (shape), std::begin (m_shape)) || force)
916930 {
@@ -938,6 +952,8 @@ namespace xt
938952 template <class S >
939953 inline void xstrided_container<D>::resize(S&& shape, layout_type l)
940954 {
955+ XTENSOR_ASSERT_MSG (detail::check_resize_dimension (m_shape, shape),
956+ " cannot change the number of dimensions of xtensor" )
941957 if (base_type::static_layout != layout_type::dynamic && l != base_type::static_layout)
942958 {
943959 XTENSOR_THROW (std::runtime_error, " Cannot change layout_type if template parameter not layout_type::dynamic." );
@@ -957,6 +973,8 @@ namespace xt
957973 template <class S >
958974 inline void xstrided_container<D>::resize(S&& shape, const strides_type& strides)
959975 {
976+ XTENSOR_ASSERT_MSG (detail::check_resize_dimension (m_shape, shape),
977+ " cannot change the number of dimensions of xtensor" )
960978 if (base_type::static_layout != layout_type::dynamic)
961979 {
962980 XTENSOR_THROW (std::runtime_error,
0 commit comments