Skip to content

Commit 3b4c217

Browse files
committed
Updated eval implementation and tests (design-docs #19)
1 parent 9c7f88d commit 3b4c217

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

stan/math/prim/fun/eval.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,30 @@ namespace stan {
1010
namespace math {
1111

1212
/**
13-
* Inputs that are not Eigen types are forwarded unmodified
13+
* Inputs which have a plain_type equal to the own time are forwarded
14+
* unmodified (for Eigen expressions these types are different)
1415
*
1516
* @tparam T Input type
1617
* @param[in] arg Input argument
1718
* @return Forwarded input argument
1819
**/
19-
template <typename T, require_not_eigen_t<T>* = nullptr>
20+
template <typename T,
21+
require_same_t<std::decay_t<T>, plain_type_t<T>>* = nullptr>
2022
inline decltype(auto) eval(T&& arg) {
2123
return std::forward<T>(arg);
2224
}
2325

2426
/**
25-
* Inputs that are Eigen types are eval'd and returned
27+
* Inputs which have a plain_type different from their own type are
28+
* Eval'd (this catches Eigen expressions)
2629
*
2730
* @tparam T Input type
2831
* @param[in] arg Input argument
2932
* @return Eval'd argument
3033
**/
31-
template <typename T, require_eigen_t<T>* = nullptr>
32-
inline decltype(auto) eval(T arg) {
34+
template <typename T,
35+
require_not_same_t<std::decay_t<T>, plain_type_t<T>>* = nullptr>
36+
inline decltype(auto) eval(const T& arg) {
3337
return arg.eval();
3438
}
3539

test/unit/math/prim/fun/eval_test.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,9 @@ TEST(MathFunctions, eval_return_type_short_circuit_std_vector) {
149149
TEST(MathFunctions, eval_return_type_short_circuit_vector_xd) {
150150
Eigen::Matrix<double, Eigen::Dynamic, 1> a(5);
151151
const Eigen::Matrix<double, Eigen::Dynamic, 1> b(5);
152-
stan::math::eval(a);
153152
EXPECT_TRUE(
154153
(std::is_same<decltype(stan::math::eval(a)),
155-
const Eigen::Matrix<double, Eigen::Dynamic, 1>&>::value));
154+
Eigen::Matrix<double, Eigen::Dynamic, 1>&>::value));
156155
EXPECT_TRUE(
157156
(std::is_same<decltype(stan::math::eval(b)),
158157
const Eigen::Matrix<double, Eigen::Dynamic, 1>&>::value));
@@ -163,7 +162,7 @@ TEST(MathFunctions, eval_return_type_short_circuit_row_vector_xd) {
163162
const Eigen::Matrix<double, 1, Eigen::Dynamic> b(5);
164163
EXPECT_TRUE(
165164
(std::is_same<decltype(stan::math::eval(a)),
166-
const Eigen::Matrix<double, 1, Eigen::Dynamic>&>::value));
165+
Eigen::Matrix<double, 1, Eigen::Dynamic>&>::value));
167166
EXPECT_TRUE(
168167
(std::is_same<decltype(stan::math::eval(b)),
169168
const Eigen::Matrix<double, 1, Eigen::Dynamic>&>::value));
@@ -173,7 +172,7 @@ TEST(MathFunctions, eval_return_type_short_circuit_matrix_xd) {
173172
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> a(5, 4);
174173
const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> b(5, 4);
175174
EXPECT_TRUE((std::is_same<decltype(stan::math::eval(a)),
176-
const Eigen::Matrix<double, Eigen::Dynamic,
175+
Eigen::Matrix<double, Eigen::Dynamic,
177176
Eigen::Dynamic>&>::value));
178177
EXPECT_TRUE((std::is_same<decltype(stan::math::eval(b)),
179178
const Eigen::Matrix<double, Eigen::Dynamic,
@@ -201,7 +200,7 @@ TEST(MathFunctions, eval_return_type_short_circuit_static_sized_matrix) {
201200
Eigen::Matrix<double, 5, 4> a;
202201
const Eigen::Matrix<double, 5, 4> b;
203202
EXPECT_TRUE((std::is_same<decltype(stan::math::eval(a)),
204-
const Eigen::Matrix<double, 5, 4>&>::value));
203+
Eigen::Matrix<double, 5, 4>&>::value));
205204
EXPECT_TRUE((std::is_same<decltype(stan::math::eval(b)),
206205
const Eigen::Matrix<double, 5, 4>&>::value));
207206
}

0 commit comments

Comments
 (0)