Compile- and run-time assertions for the construction of Ref<const>.
diff --git a/Eigen/src/Core/Ref.h b/Eigen/src/Core/Ref.h index 81de5f9..11866f7 100644 --- a/Eigen/src/Core/Ref.h +++ b/Eigen/src/Core/Ref.h
@@ -332,6 +332,16 @@ : public RefBase<Ref<const TPlainObjectType, Options, StrideType> > { typedef internal::traits<Ref> Traits; + + static constexpr bool may_map_m_object_successfully = + (StrideType::InnerStrideAtCompileTime == 0 || + StrideType::InnerStrideAtCompileTime == 1 || + StrideType::InnerStrideAtCompileTime == Dynamic) && + (TPlainObjectType::IsVectorAtCompileTime || + StrideType::OuterStrideAtCompileTime == 0 || + StrideType::OuterStrideAtCompileTime == Dynamic || + StrideType::OuterStrideAtCompileTime == TPlainObjectType::InnerSizeAtCompileTime || + TPlainObjectType::InnerSizeAtCompileTime == Dynamic); public: typedef RefBase<Ref> Base; @@ -344,6 +354,8 @@ // std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n"; // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n"; // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n"; + EIGEN_STATIC_ASSERT(Traits::template match<Derived>::type::value || may_map_m_object_successfully, + STORAGE_LAYOUT_DOES_NOT_MATCH); construct(expr.derived(), typename Traits::template match<Derived>::type()); } @@ -353,6 +365,8 @@ template<typename OtherRef> EIGEN_DEVICE_FUNC inline Ref(const RefBase<OtherRef>& other) { + EIGEN_STATIC_ASSERT(Traits::template match<OtherRef>::type::value || may_map_m_object_successfully, + STORAGE_LAYOUT_DOES_NOT_MATCH); construct(other.derived(), typename Traits::template match<OtherRef>::type()); } @@ -371,7 +385,9 @@ EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type) { internal::call_assignment_no_alias(m_object,expr,internal::assign_op<Scalar,Scalar>()); - Base::construct(m_object); + const bool success = Base::construct(m_object); + EIGEN_ONLY_USED_FOR_DEBUG(success) + eigen_assert(success); } protected:
diff --git a/failtest/CMakeLists.txt b/failtest/CMakeLists.txt index 2c5fc33..107c9e2 100644 --- a/failtest/CMakeLists.txt +++ b/failtest/CMakeLists.txt
@@ -36,6 +36,8 @@ ei_add_failtest("ref_3") ei_add_failtest("ref_4") ei_add_failtest("ref_5") +ei_add_failtest("ref_6") +ei_add_failtest("ref_7") ei_add_failtest("swap_1") ei_add_failtest("swap_2")
diff --git a/failtest/ref_6.cpp b/failtest/ref_6.cpp new file mode 100644 index 0000000..b246f97 --- /dev/null +++ b/failtest/ref_6.cpp
@@ -0,0 +1,15 @@ +#include "../Eigen/Core" + +using namespace Eigen; + +void call_ref(Ref<const VectorXf, 0, InnerStride<2>>) {} + +int main() { + VectorXf a(10); + Map<const VectorXf, 0, InnerStride<2>> m(a.data(), 5); +#ifdef EIGEN_SHOULD_FAIL_TO_BUILD + call_ref(a); +#else + call_ref(m); +#endif +}
diff --git a/failtest/ref_7.cpp b/failtest/ref_7.cpp new file mode 100644 index 0000000..7d6a3cb --- /dev/null +++ b/failtest/ref_7.cpp
@@ -0,0 +1,16 @@ +#include "../Eigen/Core" + +using namespace Eigen; + +void call_ref(Ref<const Matrix3f, 0, OuterStride<2>>) {} + +int main() { + MatrixXf a(6, 2); + Map<const Matrix3f, 0, OuterStride<Dynamic>> md(a.data(), OuterStride<Dynamic>(2)); + Map<const Matrix3f, 0, OuterStride<2>> m2(a.data()); +#ifdef EIGEN_SHOULD_FAIL_TO_BUILD + call_ref(md); +#else + call_ref(m2); +#endif +}