jeudi 28 décembre 2017

Check if all std::tuple elements meet a condition + design concerns

So I am trying to validate if all elements of a std::tuple meet a certain condition. My current solution uses C++17 fold epxressions:

template <typename F, typename Tuple, size_t...Is>
_CR_INLINE bool tuple_all_of(F&& fn, Tuple&& t, std::index_sequence<Is...>) {
    return (std::forward<F>(fn)(std::get<Is>(std::forward<Tuple>(t))) && ...);
}

template <typename F, typename Tuple>
_CR_INLINE bool tuple_all_of(F&& fn, Tuple&& t) {
    return tuple_all_of(std::forward<F>(fn), std::forward<Tuple>(t), 
        std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>());
}

It compiles in latest clang and gcc, but fails in MSVC, because fold expressions are not (yet) implemented. (http://ift.tt/2q3O3xq)

So is there a similar solution without using fold expressions?


This is how it's supposed to be used:

template<typename...Components, typename Callable>
    void FindComponents(Callable&& fn) {
        for (auto& node : m_Entities) {
            auto tp = std::make_tuple(node.GetComponent<Components>()...);
            if (!tuple_all_of([](auto& p) { return p != nullptr; }, tp))
                continue;

            apply_from_tuple(fn, tp);
        }
    }

So I am calling fn only if a node has the requested components attached (i.e. GetComponent != nullptr). Maybe there is a better solution?

Aucun commentaire:

Enregistrer un commentaire