diff --git a/stan/math/prim/meta/is_eigen.hpp b/stan/math/prim/meta/is_eigen.hpp index 2222ac6e063..480dc601330 100644 --- a/stan/math/prim/meta/is_eigen.hpp +++ b/stan/math/prim/meta/is_eigen.hpp @@ -20,6 +20,28 @@ template struct is_eigen : bool_constant::value> {}; +namespace internal { +// primary template handles types that have no nested ::type member: +template +struct has_internal_trait : std::false_type {}; + +// specialization recognizes types that do have a nested ::type member: +template +struct has_internal_trait>>> + : std::true_type {}; + +// primary template handles types that have no nested ::type member: +template +struct has_scalar_trait : std::false_type {}; + +// specialization recognizes types that do have a nested ::type member: +template +struct has_scalar_trait::Scalar>> + : std::true_type {}; + +} // namespace internal + + /** * Template metaprogram defining the base scalar type of * values stored in an Eigen matrix. @@ -28,7 +50,7 @@ struct is_eigen * @ingroup type_trait */ template -struct scalar_type::value>> { +struct scalar_type::value && internal::has_scalar_trait::value>> { using type = scalar_type_t::Scalar>; }; @@ -40,10 +62,36 @@ struct scalar_type::value>> { * @ingroup type_trait */ template -struct value_type::value>> { +struct value_type::value && internal::has_scalar_trait::value>> { + using type = typename std::decay_t::Scalar; +}; + + +/** + * Template metaprogram defining the base scalar type of + * values stored in an Eigen matrix. + * + * @tparam T type to check. + * @ingroup type_trait + */ +template +struct scalar_type::value && !internal::has_scalar_trait::value>> { + using type = scalar_type_t>::Scalar>; +}; + +/** + * Template metaprogram defining the type of values stored in an + * Eigen matrix, vector, or row vector. + * + * @tparam T type to check + * @ingroup type_trait + */ +template +struct value_type::value && !internal::has_scalar_trait::value>> { using type = typename Eigen::internal::traits>::Scalar; }; + /*! \ingroup require_eigens_types */ /*! \defgroup eigen_types eigen */ /*! \addtogroup eigen_types */ diff --git a/stan/math/rev/core/arena_matrix.hpp b/stan/math/rev/core/arena_matrix.hpp index 8913d4adc70..da9094abcb2 100644 --- a/stan/math/rev/core/arena_matrix.hpp +++ b/stan/math/rev/core/arena_matrix.hpp @@ -482,6 +482,7 @@ namespace internal { template struct traits> { using base = traits>; + using Scalar = typename base::Scalar; using XprKind = typename Eigen::internal::traits>::XprKind; enum { PlainObjectTypeInnerSize = base::PlainObjectTypeInnerSize,