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

标签 c++ c++11 tuples

所以我试图验证 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/

相关文章:

C++14 将元组三乘三展开

python - 我 pickle 了我的元组

python - 设置包含 1 个列表和列表列表的字典

c++ - 将 IOCTL 发送到 Windows 设备驱动程序 - CreateFile 失败

c++ - 仅运行一次函数的标准库函数

c++ - 无法在opencv中读取视频

c++ - 为什么这是 C++ 中的有效函数声明?

C++11 - 清除返回值语法和 decltype 关键字

c++ - 使用单元素STL容器作为缓存?

从字符串中删除括号的 C++ 函数并不能全部捕获