Skip to content

Commit b80e1b1

Browse files
authored
Make calling begin/end on non-contiguous slices a compile-time error when possible (#236)
1 parent 3fece6b commit b80e1b1

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

src/ArraySlice.hpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ class ArraySlice
141141
/**
142142
* @return Return a new immutable slice.
143143
*/
144-
template< typename U=T >
145144
LVARRAY_HOST_DEVICE inline constexpr
146145
ArraySlice< T const, NDIM, USD, INDEX_TYPE >
147146
toSliceConst() const noexcept
@@ -211,14 +210,12 @@ class ArraySlice
211210

212211
/**
213212
* @brief Check if the slice is contiguous in memory
214-
* @tparam _USD dummy template parameter equal to USD; do not replace
215213
* @return @p true if represented slice is contiguous in memory
216214
*/
217-
template< int _USD = USD >
218215
LVARRAY_HOST_DEVICE inline constexpr
219-
std::enable_if_t< ( _USD >= 0), bool >
220-
isContiguous() const
216+
bool isContiguous() const
221217
{
218+
if( USD < 0 ) return false;
222219
if( NDIM == 1 && USD == 0 ) return true;
223220

224221
bool rval = true;
@@ -235,18 +232,6 @@ class ArraySlice
235232
return rval;
236233
}
237234

238-
/**
239-
* @brief Check if the slice is contiguous in memory
240-
* @tparam USD_ dummy template parameter equal to USD; do not replace
241-
* @return @p false, this overload is enabled for slices that
242-
* have already lost its unit stride dimension
243-
*/
244-
template< int USD_ = USD >
245-
LVARRAY_HOST_DEVICE inline constexpr
246-
std::enable_if_t< (USD_ < 0), bool >
247-
isContiguous() const
248-
{ return false; }
249-
250235
/**
251236
* @tparam INDICES A variadic pack of integral types.
252237
* @return Return the linear index from a multidimensional index.
@@ -274,9 +259,9 @@ class ArraySlice
274259
* @return A raw pointer.
275260
* @note This method is only active when NDIM == 1 and USD == 0.
276261
*/
277-
template< int _NDIM=NDIM, int _USD=USD >
262+
template< int NDIM_=NDIM, int USD_=USD >
278263
LVARRAY_HOST_DEVICE constexpr inline
279-
operator std::enable_if_t< _NDIM == 1 && _USD == 0, T * const LVARRAY_RESTRICT >
264+
operator std::enable_if_t< NDIM_ == 1 && USD_ == 0, T * const LVARRAY_RESTRICT >
280265
() const noexcept
281266
{ return m_data; }
282267

@@ -325,12 +310,16 @@ class ArraySlice
325310

326311
/**
327312
* @return Return a pointer to the values.
313+
* @tparam USD_ Dummy template parameter, do not specify.
328314
* @pre The slice must be contiguous.
329315
*/
316+
template< int USD_ = USD >
330317
LVARRAY_HOST_DEVICE inline
331318
T * dataIfContiguous() const
332319
{
333-
LVARRAY_ERROR_IF( !isContiguous(), "The slice must be contiguous for direct data access" );
320+
// Note: need both compile-time and runtime checks as USD >= 0 does not guarantee contiguous data.
321+
static_assert( USD_ >= 0, "Direct data access not supported for non-contiguous slices" );
322+
LVARRAY_ERROR_IF( !isContiguous(), "Direct data access not supported for non-contiguous slices" );
334323
return m_data;
335324
}
336325

0 commit comments

Comments
 (0)