Skip to content

Commit c8843bf

Browse files
committed
[VecOps] Add sanity checks on Map args with user-friendly error
Before this commit, passing arguments to VecOps::Map in the wrong order resulted in ugly template error messages. Now the static_assert message should provide clearer diagnostics.
1 parent fdd815d commit c8843bf

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

math/vecops/inc/ROOT/RVec.hxx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ namespace VecOps {
6767
template<typename T>
6868
using RVec = ROOT::VecOps::RVec<T>;
6969

70+
// clang-format off
71+
template <typename>
72+
struct IsRVec : std::false_type {};
73+
74+
template <typename T>
75+
struct IsRVec<ROOT::VecOps::RVec<T>> : std::true_type {};
76+
// clang-format on
77+
78+
constexpr bool All(const bool *vals, std::size_t size)
79+
{
80+
for (auto i = 0u; i < size; ++i)
81+
if (!vals[i])
82+
return false;
83+
return true;
84+
}
85+
7086
template <typename... T>
7187
std::size_t GetVectorsSize(const std::string &id, const RVec<T> &... vs)
7288
{
@@ -2053,6 +2069,13 @@ auto Map(Args &&... args)
20532069
1. Forward as tuple the pack to MapFromTuple
20542070
2. Invoke the MapImpl helper which has the signature `template<...T, F> RVec MapImpl(F &&f, RVec<T>...)`
20552071
*/
2072+
2073+
// check the first N - 1 arguments are RVecs
2074+
constexpr auto nArgs = sizeof...(Args);
2075+
constexpr bool isRVec[]{ROOT::Internal::VecOps::IsRVec<std::remove_cv_t<std::remove_reference_t<Args>>>::value...};
2076+
static_assert(ROOT::Internal::VecOps::All(isRVec, nArgs - 1),
2077+
"Map: the first N-1 arguments must be RVecs or references to RVecs");
2078+
20562079
return ROOT::Internal::VecOps::MapFromTuple(std::forward_as_tuple(args...),
20572080
std::make_index_sequence<sizeof...(args) - 1>());
20582081
}

0 commit comments

Comments
 (0)