@@ -29,17 +29,7 @@ inline void check_matching_dims(const char* function, const char* name1,
29
29
std::vector<int > y1_d = dims (y1 );
30
30
std::vector<int > y2_d = dims (y2);
31
31
bool error = false ;
32
- if (y1_d.size () != y2_d.size ()) {
33
- error = true ;
34
- } else {
35
- for (int i = 0 ; i < y1_d.size (); i++) {
36
- if (y1_d[i] != y2_d[i]) {
37
- error = true ;
38
- break ;
39
- }
40
- }
41
- }
42
- if (error) {
32
+ auto error_throw = [&]() STAN_COLD_PATH {
43
33
std::ostringstream y1s;
44
34
if (y1_d.size () > 0 ) {
45
35
y1s << y1_d[0 ];
@@ -58,6 +48,15 @@ inline void check_matching_dims(const char* function, const char* name1,
58
48
msg << " ) must match in size" ;
59
49
std::string msg_str (msg.str ());
60
50
invalid_argument (function, name1, y1s.str (), " (" , msg_str.c_str ());
51
+ };
52
+ if (y1_d.size () != y2_d.size ()) {
53
+ error_throw ();
54
+ } else {
55
+ for (int i = 0 ; i < y1_d.size (); i++) {
56
+ if (y1_d[i] != y2_d[i]) {
57
+ error_throw ();
58
+ }
59
+ }
61
60
}
62
61
}
63
62
@@ -77,12 +76,14 @@ template <typename T1, typename T2, require_all_matrix_t<T1, T2>* = nullptr>
77
76
inline void check_matching_dims (const char * function, const char * name1,
78
77
const T1& y1, const char * name2, const T2& y2) {
79
78
if (y1 .rows () != y2.rows () || y1 .cols () != y2.cols ()) {
80
- std::ostringstream y1_err;
81
- std::ostringstream msg_str;
82
- y1_err << " (" << y1 .rows () << " , " << y1 .cols () << " )" ;
83
- msg_str << y2.rows () << " , " << y2.cols () << " ) must match in size" ;
84
- invalid_argument (function, name1, y1_err.str (), " (" ,
85
- std::string (msg_str.str ()).c_str ());
79
+ [&]() STAN_COLD_PATH {
80
+ std::ostringstream y1_err;
81
+ std::ostringstream msg_str;
82
+ y1_err << " (" << y1 .rows () << " , " << y1 .cols () << " )" ;
83
+ msg_str << y2.rows () << " , " << y2.cols () << " ) must match in size" ;
84
+ invalid_argument (function, name1, y1_err.str (), " (" ,
85
+ std::string (msg_str.str ()).c_str ());
86
+ }();
86
87
}
87
88
}
88
89
@@ -135,11 +136,13 @@ inline void check_matching_dims(const char* function, const char* name1,
135
136
!= static_cast <int >(Mat2::RowsAtCompileTime)
136
137
|| static_cast <int >(Mat1::ColsAtCompileTime)
137
138
!= static_cast <int >(Mat2::ColsAtCompileTime))) {
138
- std::ostringstream msg;
139
- msg << " Static rows and cols of " << name1 << " and " << name2
140
- << " must match in size." ;
141
- std::string msg_str (msg.str ());
142
- invalid_argument (function, msg_str.c_str (), " " , " " );
139
+ [&]() STAN_COLD_PATH {
140
+ std::ostringstream msg;
141
+ msg << " Static rows and cols of " << name1 << " and " << name2
142
+ << " must match in size." ;
143
+ std::string msg_str (msg.str ());
144
+ invalid_argument (function, msg_str.c_str (), " " , " " );
145
+ }();
143
146
}
144
147
check_matching_dims (function, name1, y1 , name2, y2);
145
148
}
0 commit comments