gpt4 book ai didi

c++ - 检查所有 std::tuple 元素是否满足条件+设计问题

转载 作者:行者123 更新时间:2023-11-28 01:39:15 24 4
gpt4 key购买 nike

所以我试图验证 std::tuple 的所有元素是否满足特定条件。我当前的解决方案使用 C++17 折叠表达式:

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>>>());
}

它在最新的 clang 和 gcc 中编译,但在 MSVC 中失败,因为折叠表达式尚未(尚未)实现。 (https://blogs.msdn.microsoft.com/vcblog/2017/05/10/c17-features-in-vs-2017-3/)

那么有没有不使用折叠表达式的类似解决方案呢?


这是它应该如何使用:

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);
}
}

因此,仅当节点附加了请求的组件(即 GetComponent != nullptr)时,我才调用 fn。也许有更好的解决方案?

最佳答案

根据 Visual C++ Language Conformance使用 /std:c++17(或 /std:c++latest)编译器选项从 VS2017 15.5 开始支持折叠表达式。

关于c++ - 检查所有 std::tuple 元素是否满足条件+设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48013094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com