Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ class Array : public ArrayView< T,
return *this;
}

/**
* @brief Copy assignment operator, performs a deep copy of rhs.
* @param rhs Source for the assignment.
* @return *this.
*/
LVARRAY_HOST_DEVICE
Array & operator=( typename ParentClass::ViewTypeConst const & rhs )
{
bufferManipulation::copyInto( this->m_dataBuffer, this->size(), rhs.dataBuffer(), rhs.size() );

INDEX_TYPE const * const dims = rhs.dims();
INDEX_TYPE const * const strides = rhs.strides();
for( int i = 0; i < NDIM; ++i )
{
this->m_dims[ i ] = dims[ i ];
this->m_strides[ i ] = strides[ i ];
}

setSingleParameterResizeIndex( rhs.getSingleParameterResizeIndex() );
return *this;
}

/**
* @brief Move assignment operator, performs a shallow copy of rhs.
* @param rhs Source for the assignment.
Expand Down
14 changes: 9 additions & 5 deletions src/ArrayView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class ArrayView
/// The integer type used for indexing.
using IndexType = INDEX_TYPE;

/// The type when the data type is const.
using ViewTypeConst = ArrayView< std::remove_const_t< T > const, NDIM, USD, INDEX_TYPE, BUFFER_TYPE >;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious but is the std::remove_const necessary? I thought you don't have problems with duplicate const.

Copy link
Member Author

@rrsettgast rrsettgast Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had put in ArrayVIew<T>::operator=( ArrayView<T> const&), ArrayVIew<T>::operator=( ViewTypeConst const &), but then thought better of it. I felt it was a little unclear what the intent of the operator is, and whether it would be a source of bugs. I did put in the std::remove_const because I was getting some errors in the using, but now I am not sure I was this is what fixed the problem I was having. Also:

https://stackoverflow.com/questions/5781222/duplicate-const-qualifier-allowed-in-c-but-not-in-c

but when I remove the std::remove_const it compiles fine now. I could remove it, but I feel it is better to be explicit about it...not that this is valid reasoning.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...but then again, if I do something like this:

  array1d<double> a;
  arrayView1d<double const> aView = a;
  arrayView1d<double const>::ViewTypeConst anotherView = a;

  std::cout<<LvArray::system::demangle(typeid(anotherView).name())<<std::endl;

it outputs:

LvArray::ArrayView<double const, 1, 0, int, LvArray::ChaiBuffer>

So it is dropping the redundant const. If you dry to do:

double const const a = 1.0;

you get:

/usr/WS2/settgast/Codes/geosx/GEOSX_wave/src/main/main.cpp:34:14: error: duplicate 'const' declaration specifier [-Werror,-Wduplicate-decl-specifier]
double const const aScalar = 1.0;

so looks like it is illegal, but the compiler drops the second const when it is an alias??

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in http://eel.is/c++draft/dcl.type.cv#1 this sentence

Each cv-qualifier shall appear at most once in a cv-qualifier-seq.

refers to language grammar, i.e. what goes into text of a program, so it prohibits literally typing const const anywhere. But then

Redundant cv-qualifications are ignored.
[Note [2]: For example, these could be introduced by typedefs. — end note]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had put in ArrayVIew<T>::operator=( ArrayView<T> const&), ArrayVIew<T>::operator=( ViewTypeConst const &), but then thought better of it

We rely on the shallow copy semantics of ArrayView in at least once place where we construct the element accessors. Plus it'd be weird to have the copy constructor do something so different from the assignment operator.



/// The type when all inner array classes are converted to const views.
using NestedViewType = ArrayView< std::remove_reference_t< typeManipulation::NestedViewType< T > >,
NDIM, USD, INDEX_TYPE, BUFFER_TYPE >;
Expand Down Expand Up @@ -274,12 +278,12 @@ class ArrayView
* @return Return a new ArrayView where @c T is @c const.
*/
inline LVARRAY_HOST_DEVICE constexpr
ArrayView< T const, NDIM, USD, INDEX_TYPE, BUFFER_TYPE > toViewConst() const &
ViewTypeConst toViewConst() const &
{
return ArrayView< T const, NDIM, USD, INDEX_TYPE, BUFFER_TYPE >( m_dims,
m_strides,
m_singleParameterResizeIndex,
m_dataBuffer );
return ViewTypeConst( m_dims,
m_strides,
m_singleParameterResizeIndex,
m_dataBuffer );
}

/**
Expand Down