Make fixed-size Matrix and Array trivially copyable after C++20

Making them trivially copyable allows using std::memcpy() without undefined
behaviors.

Only Matrix and Array with trivially copyable DenseStorage are marked as
trivially copyable with an additional type trait.

As described in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0848r3.html
it requires extremely verbose SFINAE to make the special member functions of
fixed-size Matrix and Array trivial, unless C++20 concepts are available to
simplify the selection of trivial special member functions given template
parameters. Therefore only make this feature available to compilers that support
C++20 P0848R3.

Fix #1855.
This commit is contained in:
Lingzhu Xiang
2022-01-07 19:04:35 +00:00
committed by David Tellenbach
parent c4b1dd2f6b
commit 47eac21072
11 changed files with 127 additions and 2 deletions

View File

@@ -463,6 +463,12 @@ class PlainObjectBase : public internal::dense_xpr_base<Derived>::type
return _set(other);
}
#if EIGEN_COMP_HAS_P0848R3
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE PlainObjectBase& operator=(
const PlainObjectBase& other) requires internal::has_trivially_copyable_storage<Derived>::value = default;
#endif
/** \sa MatrixBase::lazyAssign() */
template<typename OtherDerived>
EIGEN_DEVICE_FUNC
@@ -514,10 +520,27 @@ class PlainObjectBase : public internal::dense_xpr_base<Derived>::type
return *this;
}
#if EIGEN_COMP_HAS_P0848R3
EIGEN_DEVICE_FUNC
PlainObjectBase(PlainObjectBase&& other) EIGEN_NOEXCEPT
requires internal::has_trivially_copyable_storage<Derived>::value = default;
EIGEN_DEVICE_FUNC
PlainObjectBase& operator=(PlainObjectBase&& other) EIGEN_NOEXCEPT
requires internal::has_trivially_copyable_storage<Derived>::value = default;
#endif
/** Copy constructor */
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE PlainObjectBase(const PlainObjectBase& other)
: Base(), m_storage(other.m_storage) { }
#if EIGEN_COMP_HAS_P0848R3
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE PlainObjectBase(
const PlainObjectBase& other) requires internal::has_trivially_copyable_storage<Derived>::value = default;
#endif
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE PlainObjectBase(Index size, Index rows, Index cols)
: m_storage(size, rows, cols)