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: protected:
enum { IsFixedSizeVectorAtCompileTime = RowsAtCompileTime != Dynamic && ColsAtCompileTime != Dynamic && IsVectorAtCompileTime == 1 }; enum { IsFixedSizeVectorAtCompileTime = RowsAtCompileTime != Dynamic && ColsAtCompileTime != Dynamic && IsVectorAtCompileTime == 1 };
public: public:
template<typename T, template <typename... ArgTypes>
typename = typename internal::enable_if<IsFixedSizeVectorAtCompileTime && internal::is_same<T, Scalar>::value>::type> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
EIGEN_DEVICE_FUNC Array(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
explicit EIGEN_STRONG_INLINE Array(const std::initializer_list<T>& list) : Base(list) {} : Base(a0, a1, a2, a3, args...) {}
EIGEN_DEVICE_FUNC EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE Array(const std::initializer_list<std::initializer_list<Scalar> >& list) : Base(list) {} 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 */ /** constructs an initialized 2D vector with given coefficients */
Array(const Scalar& val0, const Scalar& val1); Array(const Scalar& val0, const Scalar& val1);
/** \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 Array_initializer_list2_cxx11.cpp * Example: \include Array_variadic_ctor_cxx11.cpp
* Output: \verbinclude Array_initializer_list2_cxx11.out * Output: \verbinclude Array_variadic_ctor_cxx11.out
* *
* \sa Array(const std::initializer_list<std::initializer_list<Scalar> >&) * \sa Array(const std::initializer_list<std::initializer_list<Scalar>>&)
*/ */
EIGEN_DEVICE_FUNC template <typename... ArgTypes>
explicit EIGEN_STRONG_INLINE Array(const std::initializer_list<Scalar>& list); 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 /** \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 #if EIGEN_HAS_CXX11
protected:
enum { IsFixedSizeVectorAtCompileTime = RowsAtCompileTime != Dynamic && ColsAtCompileTime != Dynamic && IsVectorAtCompileTime == 1 };
public: public:
template<typename T, template <typename... ArgTypes>
typename = typename internal::enable_if<IsFixedSizeVectorAtCompileTime && internal::is_same<T, Scalar>::value>::type> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
EIGEN_DEVICE_FUNC Matrix(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list<T>& list) : Base(list) {} : Base(a0, a1, a2, a3, args...) {}
EIGEN_DEVICE_FUNC EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list<std::initializer_list<Scalar>>& list) : Base(list) {} 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 */ /** \brief Constructs an initialized 2D vector with given coefficients */
Matrix(const Scalar& x, const Scalar& y); 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 * Example: \include Matrix_variadic_ctor_cxx11.cpp
* Output: \verbinclude Matrix_initializer_list2_cxx11.out * Output: \verbinclude Matrix_variadic_ctor_cxx11.out
* *
* \sa Matrix(const std::initializer_list<std::initializer_list<Scalar>>&) * \sa Matrix(const std::initializer_list<std::initializer_list<Scalar>>&)
*/ */
EIGEN_DEVICE_FUNC template <typename... ArgTypes>
explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list<Scalar>& list); 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 /** \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 #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 * \only_for_vectors
* *
* \warning To construct a column (resp. row) vector of fixed length, the number of values passed through * \warning To construct a column (resp. row) vector of fixed length, the number of values passed to this
* the initializer list must match the the fixed number of rows (resp. columns) of \c *this. * constructor must match the the fixed number of rows (resp. columns) of \c *this.
*/ */
EIGEN_DEVICE_FUNC template <typename... ArgTypes>
explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list<Scalar>& list); 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 /** \brief Constructs a Matrix or Array and initializes it by elements given by an initializer list of initializer
* lists \cpp11 * 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); explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list<std::initializer_list<Scalar>>& list);
#else // EIGEN_PARSED_BY_DOXYGEN #else // EIGEN_PARSED_BY_DOXYGEN
#if EIGEN_HAS_CXX11 #if EIGEN_HAS_CXX11
template<typename T>
EIGEN_DEVICE_FUNC protected:
explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list<T>& list, enum { IsFixedSizeVectorAtCompileTime = RowsAtCompileTime != Dynamic && ColsAtCompileTime != Dynamic && IsVectorAtCompileTime == 1 };
typename internal::enable_if<internal::is_same<T, Scalar>::value, T>::type* = 0, public:
typename internal::enable_if<RowsAtCompileTime != Dynamic
&& ColsAtCompileTime != Dynamic template <typename... ArgTypes>
&& IsVectorAtCompileTime == 1, T>::type* = 0) EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
PlainObjectBase(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
: m_storage() : m_storage()
{ {
_check_template_params(); _check_template_params();
EIGEN_STATIC_ASSERT_FIXED_SIZE(PlainObjectBase); EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(PlainObjectBase, sizeof...(args) + 4);
resize(list.size()); m_storage.data()[0] = a0;
std::copy(list.begin(), list.end(), m_storage.data()); 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 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); Vector4d c(5.0, 6.0, 7.0, 8.0);
\endcode \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 \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, 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 \endcode
In the general case of matrices and vectors with either fixed or runtime sizes, 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}; Array<int, 3, 1> b {1, 2, 3};
cout << a << "\n\n" << b << endl; 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}; Matrix<int, 3, 1> b {1, 2, 3};
cout << a << "\n\n" << b << endl; cout << a << "\n\n" << b << endl;