Skip to content

Commit 613ae98

Browse files
committed
Added compilation errors for certain operations on rvalues.
1 parent e176131 commit 613ae98

21 files changed

+918
-223
lines changed

src/Array.hpp

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ class Array : public ArrayView< T,
7171
using Permutation = PERMUTATION;
7272

7373
/// Alias for the parent class.
74-
using ParentClass = ArrayView< T,
75-
NDIM,
76-
typeManipulation::getStrideOneDimension( PERMUTATION {} ),
77-
INDEX_TYPE,
78-
BUFFER_TYPE >;
74+
using ParentClass = ArrayView< T, NDIM, typeManipulation::getStrideOneDimension( Permutation {} ), INDEX_TYPE, BUFFER_TYPE >;
75+
76+
using ParentClass::USD;
77+
using typename ParentClass::NestedViewType;
78+
using typename ParentClass::NestedViewTypeConst;
7979

8080
/**
8181
* @name Constructors, destructor and assignment operators.
@@ -186,6 +186,77 @@ class Array : public ArrayView< T,
186186

187187
///@}
188188

189+
/**
190+
* @name ArrayView and ArraySlice creation methods and user defined conversions.
191+
*/
192+
///@{
193+
194+
using ParentClass::toView;
195+
196+
/**
197+
* @brief Overload for rvalues that raises a compilation error when used.
198+
* @return A null ArrayView.
199+
* @note This cannot be called on a rvalue since the @c ArrayView would
200+
* contain the buffer of the current @c Array that is about to be destroyed.
201+
* This overload prevents that from happening.
202+
*/
203+
inline LVARRAY_HOST_DEVICE constexpr
204+
ArrayView< T, NDIM, USD, INDEX_TYPE, BUFFER_TYPE > toView() const &&
205+
{
206+
static_assert( !typeManipulation::always_true< T >, "Cannot call toView on a rvalue." );
207+
return ArrayView< T, NDIM, USD, INDEX_TYPE, BUFFER_TYPE >();
208+
}
209+
210+
using ParentClass::toViewConst;
211+
212+
/**
213+
* @brief Overload for rvalues that raises a compilation error when used.
214+
* @return A null ArrayView.
215+
* @note This cannot be called on a rvalue since the @c ArrayView would
216+
* contain the buffer of the current @c Array that is about to be destroyed.
217+
* This overload prevents that from happening.
218+
*/
219+
inline LVARRAY_HOST_DEVICE constexpr
220+
ArrayView< T const, NDIM, USD, INDEX_TYPE, BUFFER_TYPE > toViewConst() const &&
221+
{
222+
static_assert( !typeManipulation::always_true< T >, "Cannot call toViewConst on a rvalue." );
223+
return ArrayView< T const, NDIM, USD, INDEX_TYPE, BUFFER_TYPE >();
224+
}
225+
226+
using ParentClass::toNestedView;
227+
228+
/**
229+
* @brief Overload for rvalues that raises a compilation error when used.
230+
* @return A null ArrayView.
231+
* @note This cannot be called on a rvalue since the @c ArrayView would
232+
* contain the buffer of the current @c Array that is about to be destroyed.
233+
* This overload prevents that from happening.
234+
*/
235+
inline LVARRAY_HOST_DEVICE constexpr
236+
NestedViewType toNestedView() const &&
237+
{
238+
static_assert( !typeManipulation::always_true< T >, "Cannot call toViewNested on a rvalue." );
239+
return NestedViewType();
240+
}
241+
242+
using ParentClass::toNestedViewConst;
243+
244+
/**
245+
* @brief Overload for rvalues that raises a compilation error when used.
246+
* @return A null ArrayView.
247+
* @note This cannot be called on a rvalue since the @c ArrayView would
248+
* contain the buffer of the current @c Array that is about to be destroyed.
249+
* This overload prevents that from happening.
250+
*/
251+
inline LVARRAY_HOST_DEVICE constexpr
252+
NestedViewTypeConst toNestedViewConst() const &&
253+
{
254+
static_assert( !typeManipulation::always_true< T >, "Cannot call toViewNestedConst on a rvalue." );
255+
return NestedViewTypeConst();
256+
}
257+
258+
///@}
259+
189260
/**
190261
* @name Resizing methods.
191262
*/

src/ArrayOfArrays.hpp

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
5252
* @param defaultArrayCapacity the initial capacity of each array.
5353
*/
5454
inline
55-
ArrayOfArrays( INDEX_TYPE const numArrays=0, INDEX_TYPE const defaultArrayCapacity=0 ) LVARRAY_RESTRICT_THIS:
56-
ParentClass()
55+
ArrayOfArrays( INDEX_TYPE const numArrays=0, INDEX_TYPE const defaultArrayCapacity=0 ):
56+
ParentClass( true )
5757
{
5858
resize( numArrays, defaultArrayCapacity );
5959
setName( "" );
@@ -65,7 +65,7 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
6565
*/
6666
inline
6767
ArrayOfArrays( ArrayOfArrays const & src ):
68-
ParentClass()
68+
ParentClass( true )
6969
{ *this = src; }
7070

7171
/**
@@ -95,9 +95,24 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
9595
*/
9696
constexpr inline
9797
ArrayOfArraysView< T, INDEX_TYPE const, false, BUFFER_TYPE >
98-
toView() const LVARRAY_RESTRICT_THIS
98+
toView() const &
9999
{ return ParentClass::toView(); }
100100

101+
/**
102+
* @brief Overload for rvalues that raises a compilation error when used.
103+
* @return A null ArrayOfArraysView.
104+
* @note This cannot be called on a rvalue since the @c ArrayOfArraysView would
105+
* contain the buffer of the current @c ArrayOfArrays that is about to be destroyed.
106+
* This overload prevents that from happening.
107+
*/
108+
constexpr inline
109+
ArrayOfArraysView< T, INDEX_TYPE const, false, BUFFER_TYPE >
110+
toView() const &&
111+
{
112+
static_assert( !typeManipulation::always_true< T >, "Cannot call toView on a rvalue." );
113+
return ParentClass::toView();
114+
}
115+
101116
/**
102117
* @copydoc ParentClass::toViewConstSizes
103118
* @note This is just a wrapper around the ArrayOfArraysView method. The reason
@@ -106,9 +121,24 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
106121
*/
107122
constexpr inline
108123
ArrayOfArraysView< T, INDEX_TYPE const, true, BUFFER_TYPE >
109-
toViewConstSizes() const LVARRAY_RESTRICT_THIS
124+
toViewConstSizes() const &
110125
{ return ParentClass::toViewConstSizes(); }
111126

127+
/**
128+
* @brief Overload for rvalues that raises a compilation error when used.
129+
* @return A null ArrayOfArraysView.
130+
* @note This cannot be called on a rvalue since the @c ArrayOfArraysView would
131+
* contain the buffer of the current @c ArrayOfArrays that is about to be destroyed.
132+
* This overload prevents that from happening.
133+
*/
134+
constexpr inline
135+
ArrayOfArraysView< T, INDEX_TYPE const, true, BUFFER_TYPE >
136+
toViewConstSizes() const &&
137+
{
138+
static_assert( !typeManipulation::always_true< T >, "Cannot call toViewConstSizes on a rvalue." );
139+
return ParentClass::toViewConstSizes();
140+
}
141+
112142
/**
113143
* @copydoc ParentClass::toViewConst
114144
* @note This is just a wrapper around the ArrayOfArraysView method. The reason
@@ -117,9 +147,24 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
117147
*/
118148
constexpr inline
119149
ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE >
120-
toViewConst() const LVARRAY_RESTRICT_THIS
150+
toViewConst() const &
121151
{ return ParentClass::toViewConst(); }
122152

153+
/**
154+
* @brief Overload for rvalues that raises a compilation error when used.
155+
* @return A null ArrayOfArraysView.
156+
* @note This cannot be called on a rvalue since the @c ArrayOfArraysView would
157+
* contain the buffer of the current @c ArrayOfArrays that is about to be destroyed.
158+
* This overload prevents that from happening.
159+
*/
160+
constexpr inline
161+
ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE >
162+
toViewConst() const &&
163+
{
164+
static_assert( !typeManipulation::always_true< T >, "Cannot call toViewConst on a rvalue." );
165+
return ParentClass::toViewConst();
166+
}
167+
123168
///@}
124169

125170
/**
@@ -133,7 +178,7 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
133178
* @return *this.
134179
*/
135180
inline
136-
ArrayOfArrays & operator=( ArrayOfArrays const & src ) LVARRAY_RESTRICT_THIS
181+
ArrayOfArrays & operator=( ArrayOfArrays const & src )
137182
{
138183
ParentClass::setEqualTo( src.m_numArrays,
139184
src.m_offsets[ src.m_numArrays ],
@@ -183,7 +228,7 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
183228
* IS_VALID_EXPRESSION and this fails with NVCC.
184229
*/
185230
inline
186-
INDEX_TYPE size() const LVARRAY_RESTRICT_THIS
231+
INDEX_TYPE size() const
187232
{ return ParentClass::size(); }
188233

189234
using ParentClass::sizeOfArray;
@@ -233,7 +278,7 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
233278
* @brief Append an array.
234279
* @param n the size of the array.
235280
*/
236-
void appendArray( INDEX_TYPE const n ) LVARRAY_RESTRICT_THIS
281+
void appendArray( INDEX_TYPE const n )
237282
{
238283
LVARRAY_ASSERT( arrayManipulation::isPositive( n ) );
239284

@@ -252,7 +297,7 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
252297
* @param last An iterator to the end of the array to append.
253298
*/
254299
template< typename ITER >
255-
void appendArray( ITER const first, ITER const last ) LVARRAY_RESTRICT_THIS
300+
void appendArray( ITER const first, ITER const last )
256301
{
257302
INDEX_TYPE const maxOffset = this->m_offsets[ this->m_numArrays ];
258303
bufferManipulation::emplaceBack( this->m_offsets, this->m_numArrays + 1, maxOffset );
@@ -312,7 +357,7 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
312357
* @param args A variadic pack of arguments that are forwarded to construct the new value.
313358
*/
314359
template< typename ... ARGS >
315-
void emplaceBack( INDEX_TYPE const i, ARGS && ... args ) LVARRAY_RESTRICT_THIS
360+
void emplaceBack( INDEX_TYPE const i, ARGS && ... args )
316361
{
317362
dynamicallyGrowArray( i, 1 );
318363
ParentClass::emplaceBack( i, std::forward< ARGS >( args )... );
@@ -326,7 +371,7 @@ class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_
326371
* @param last An iterator to the end of the values to append.
327372
*/
328373
template< typename ITER >
329-
void appendToArray( INDEX_TYPE const i, ITER const first, ITER const last ) LVARRAY_RESTRICT_THIS
374+
void appendToArray( INDEX_TYPE const i, ITER const first, ITER const last )
330375
{
331376
INDEX_TYPE const n = arrayManipulation::iterDistance( first, last );
332377
dynamicallyGrowArray( i, n );

0 commit comments

Comments
 (0)