From 218a7b98401aee558b96e21d8ba90641eb3e9371 Mon Sep 17 00:00:00 2001 From: Eugene Zhulenev <ezhulenev@google.com> Date: Tue, 18 Sep 2018 10:57:00 -0700 Subject: [PATCH 1/2] Enable DSizes type promotion with c++03 compilers --- .../Eigen/CXX11/src/Tensor/TensorDimensions.h | 20 ++++++++++--------- unsupported/test/cxx11_tensor_dimension.cpp | 7 ++++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h b/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h index 5de0d0de7..dbf5af094 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h @@ -290,22 +290,24 @@ struct DSizes : array<DenseIndex, NumDims> { } } -#ifdef EIGEN_HAS_CXX11 // Enable DSizes index type promotion only if we are promoting to the // larger type, e.g. allow to promote dimensions of type int to long. - template<typename OtherIndex, - typename std::enable_if< - std::is_same< - DenseIndex, - typename internal::promote_index_type<DenseIndex, OtherIndex>::type - >::value, int>::type = 0> + template<typename OtherIndex> EIGEN_DEVICE_FUNC - explicit DSizes(const array<OtherIndex, NumDims>& other) { + explicit DSizes(const array<OtherIndex, NumDims>& other, + // Default template parameters require c++11. + typename internal::enable_if< + internal::is_same< + DenseIndex, + typename internal::promote_index_type< + DenseIndex, + OtherIndex + >::type + >::value, void*>::type = 0) { for (int i = 0; i < NumDims; ++i) { (*this)[i] = static_cast<DenseIndex>(other[i]); } } -#endif // EIGEN_HAS_CXX11 #ifdef EIGEN_HAS_INDEX_LIST template <typename FirstType, typename... OtherTypes> diff --git a/unsupported/test/cxx11_tensor_dimension.cpp b/unsupported/test/cxx11_tensor_dimension.cpp index 26f8edd8a..ee416e14a 100644 --- a/unsupported/test/cxx11_tensor_dimension.cpp +++ b/unsupported/test/cxx11_tensor_dimension.cpp @@ -61,9 +61,11 @@ static void test_rank_zero() } static void test_index_type_promotion() { -#ifdef EIGEN_HAS_CXX11 Eigen::DSizes<int, 3> src0(1, 2, 3); - Eigen::array<int, 3> src1 = {4, 5, 6}; + Eigen::array<int, 3> src1; + src1[0] = 4; + src1[1] = 5; + src1[2] = 6; Eigen::DSizes<long, 3> dst0(src0); Eigen::DSizes<long, 3> dst1(src1); @@ -74,7 +76,6 @@ static void test_index_type_promotion() { VERIFY_IS_EQUAL(dst1[0], 4L); VERIFY_IS_EQUAL(dst1[1], 5L); VERIFY_IS_EQUAL(dst1[2], 6L); -#endif // EIGEN_HAS_CXX11 } EIGEN_DECLARE_TEST(cxx11_tensor_dimension) From c4627039ac217ea1415b43fc07cc98c8e90c1ada Mon Sep 17 00:00:00 2001 From: Eugene Zhulenev <ezhulenev@google.com> Date: Tue, 18 Sep 2018 14:25:21 -0700 Subject: [PATCH 2/2] Support static dimensions (aka IndexList) in Tensor::resize(...) --- unsupported/Eigen/CXX11/src/Tensor/Tensor.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/unsupported/Eigen/CXX11/src/Tensor/Tensor.h b/unsupported/Eigen/CXX11/src/Tensor/Tensor.h index aed71b265..95349d0ed 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/Tensor.h +++ b/unsupported/Eigen/CXX11/src/Tensor/Tensor.h @@ -477,6 +477,18 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp // Nothing to do: rank 0 tensors have fixed size } +#ifdef EIGEN_HAS_INDEX_LIST + template <typename FirstType, typename... OtherTypes> + EIGEN_DEVICE_FUNC + void resize(const Eigen::IndexList<FirstType, OtherTypes...>& dimensions) { + array<Index, NumIndices> dims; + for (int i = 0; i < NumIndices; ++i) { + dims[i] = static_cast<Index>(dimensions[i]); + } + resize(dims); + } +#endif + /** Custom Dimension */ #ifdef EIGEN_HAS_SFINAE template<typename CustomDimension,