PR 574: use variadic template instead of initializer_list to implement fixed-size vector ctor from coefficients.

This commit is contained in:
David Tellenbach 2019-01-23 00:07:19 +01:00
parent bd6dadcda8
commit 237b03b372
6 changed files with 51 additions and 44 deletions

View File

@ -183,10 +183,10 @@ class Array
protected:
enum { IsFixedSizeVectorAtCompileTime = RowsAtCompileTime != Dynamic && ColsAtCompileTime != Dynamic && IsVectorAtCompileTime == 1 };
public:
template<typename T,
typename = typename internal::enable_if<IsFixedSizeVectorAtCompileTime && internal::is_same<T, Scalar>::value>::type>
EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE Array(const std::initializer_list<T>& list) : Base(list) {}
template <typename... ArgTypes>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
Array(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
: Base(a0, a1, a2, a3, args...) {}
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE Array(const std::initializer_list<std::initializer_list<Scalar> >& list) : Base(list) {}
@ -214,15 +214,16 @@ class Array
/** constructs an initialized 2D vector with given coefficients */
Array(const Scalar& val0, const Scalar& val1);
/** \copydoc PlainObjectBase::PlainObjectBase(const std::initializer_list<Scalar>& list)
*
* Example: \include Array_initializer_list2_cxx11.cpp
* Output: \verbinclude Array_initializer_list2_cxx11.out
*
* \sa Array(const std::initializer_list<std::initializer_list<Scalar> >&)
*/
EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE Array(const std::initializer_list<Scalar>& list);
/** \copydoc PlainObjectBase(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
*
* Example: \include Array_variadic_ctor_cxx11.cpp
* Output: \verbinclude Array_variadic_ctor_cxx11.out
*
* \sa Array(const std::initializer_list<std::initializer_list<Scalar>>&)
*/
template <typename... ArgTypes>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
Array(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args);
/** \brief Constructs an array and initializes it from the coefficients given as initializer-lists grouped by row. \cpp11
*

View File

@ -303,13 +303,11 @@ class Matrix
}
#if EIGEN_HAS_CXX11
protected:
enum { IsFixedSizeVectorAtCompileTime = RowsAtCompileTime != Dynamic && ColsAtCompileTime != Dynamic && IsVectorAtCompileTime == 1 };
public:
template<typename T,
typename = typename internal::enable_if<IsFixedSizeVectorAtCompileTime && internal::is_same<T, Scalar>::value>::type>
EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list<T>& list) : Base(list) {}
template <typename... ArgTypes>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
Matrix(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
: Base(a0, a1, a2, a3, args...) {}
EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list<std::initializer_list<Scalar>>& list) : Base(list) {}
@ -353,15 +351,16 @@ class Matrix
/** \brief Constructs an initialized 2D vector with given coefficients */
Matrix(const Scalar& x, const Scalar& y);
/** \copydoc PlainObjectBase::PlainObjectBase(const std::initializer_list<Scalar>& list)
/** \copydoc PlainObjectBase(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
*
* Example: \include Matrix_initializer_list2_cxx11.cpp
* Output: \verbinclude Matrix_initializer_list2_cxx11.out
* Example: \include Matrix_variadic_ctor_cxx11.cpp
* Output: \verbinclude Matrix_variadic_ctor_cxx11.out
*
* \sa Matrix(const std::initializer_list<std::initializer_list<Scalar>>&)
*/
EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list<Scalar>& list);
template <typename... ArgTypes>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
Matrix(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args);
/** \brief Constructs a Matrix and initializes it from the coefficients given as initializer-lists grouped by row. \cpp11
*

View File

@ -527,15 +527,16 @@ class PlainObjectBase : public internal::dense_xpr_base<Derived>::type
}
#ifdef EIGEN_PARSED_BY_DOXYGEN
/** \brief Construct a row of column vector with fixed size from an initializer list of coefficients. \cpp11
/** \brief Construct a row of column vector with fixed size from an arbitrary number of coefficients. \cpp11
*
* \only_for_vectors
*
* \warning To construct a column (resp. row) vector of fixed length, the number of values passed through
* the initializer list must match the the fixed number of rows (resp. columns) of \c *this.
* \warning To construct a column (resp. row) vector of fixed length, the number of values passed to this
* constructor must match the the fixed number of rows (resp. columns) of \c *this.
*/
EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list<Scalar>& list);
template <typename... ArgTypes>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
PlainObjectBase(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args);
/** \brief Constructs a Matrix or Array and initializes it by elements given by an initializer list of initializer
* lists \cpp11
@ -544,19 +545,25 @@ class PlainObjectBase : public internal::dense_xpr_base<Derived>::type
explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list<std::initializer_list<Scalar>>& list);
#else // EIGEN_PARSED_BY_DOXYGEN
#if EIGEN_HAS_CXX11
template<typename T>
EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list<T>& list,
typename internal::enable_if<internal::is_same<T, Scalar>::value, T>::type* = 0,
typename internal::enable_if<RowsAtCompileTime != Dynamic
&& ColsAtCompileTime != Dynamic
&& IsVectorAtCompileTime == 1, T>::type* = 0)
protected:
enum { IsFixedSizeVectorAtCompileTime = RowsAtCompileTime != Dynamic && ColsAtCompileTime != Dynamic && IsVectorAtCompileTime == 1 };
public:
template <typename... ArgTypes>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
PlainObjectBase(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
: m_storage()
{
_check_template_params();
EIGEN_STATIC_ASSERT_FIXED_SIZE(PlainObjectBase);
resize(list.size());
std::copy(list.begin(), list.end(), m_storage.data());
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(PlainObjectBase, sizeof...(args) + 4);
m_storage.data()[0] = a0;
m_storage.data()[1] = a1;
m_storage.data()[2] = a2;
m_storage.data()[3] = a3;
int i = 4;
auto x = {(m_storage.data()[i++] = args, 0)...};
static_cast<void>(x);
}
EIGEN_DEVICE_FUNC

View File

@ -109,11 +109,11 @@ Vector3d b(5.0, 6.0, 7.0);
Vector4d c(5.0, 6.0, 7.0, 8.0);
\endcode
If C++11 is enabled, fixed-size column or row vectors of arbitrary size can be initialized through a single initializer list (\link Matrix::Matrix(const std::initializer_list<Scalar>&) details \endlink):
If C++11 is enabled, fixed-size column or row vectors of arbitrary size can be initialized by passing an arbitrary number of coefficients:
\code
Vector2i a {1, 2}; // A column vector containing the elements {1, 2}
Vector2i a(1, 2); // A column vector containing the elements {1, 2}
Matrix<int, 5, 1> b {1, 2, 3, 4, 5}; // A row-vector containing the elements {1, 2, 3, 4, 5}
Matrix<int, 1, 5> c {1, 2, 3, 4, 5}; // A column vector containing the elements {1, 2, 3, 4, 5}
Matrix<int, 1, 5> c = {1, 2, 3, 4, 5}; // A column vector containing the elements {1, 2, 3, 4, 5}
\endcode
In the general case of matrices and vectors with either fixed or runtime sizes,

View File

@ -1,3 +1,3 @@
Array<int, 1, 6> a {1, 2, 3, 4, 5, 6};
Array<int, 1, 6> a(1, 2, 3, 4, 5, 6);
Array<int, 3, 1> b {1, 2, 3};
cout << a << "\n\n" << b << endl;

View File

@ -1,3 +1,3 @@
Matrix<int, 1, 6> a {1, 2, 3, 4, 5, 6};
Matrix<int, 1, 6> a(1, 2, 3, 4, 5, 6);
Matrix<int, 3, 1> b {1, 2, 3};
cout << a << "\n\n" << b << endl;