c++ - 遍历多个枚举

标签 c++ c++11

我将如何遍历多个枚举。

我有点想在骨架中使用枚举

不太确定如何构建我的枚举以将它们放在这样的容器中

然后不太确定如何循环它来获取每种类型

最佳答案

我得到了一种基于元组的元编程方法

#include <tuple>
#include <type_traits>
#include <utility>
#include <iostream>

struct foo_enumerator {

enum class foo {
    ONE = 0 ,
    TWO =  1,
    THREE = 2
};

static constexpr auto reflect = std::make_tuple(
                                            foo::ONE,
                                            foo::TWO,
                                            foo::THREE);

};

struct bar_enumerator {
    enum class bar {
        FOUR = 4,
        FIVE =  5,
        SIX = 6
    };

    static constexpr auto reflect = std::make_tuple(
                                            bar::FOUR,
                                            bar::FIVE,
                                            bar::SIX);

};

// a tuple for_each implementation
// can be replaced with something else, like boost hana for_each for example
namespace detail {

// workaround for default non-type template arguments
template<std::size_t I>
using index_t = std::integral_constant<std::size_t, I>;

// process the `From::value`-th element
template<typename FromIndex,
         typename ToIndex,
         typename Tuple,
         typename UnaryFunction>
struct for_each_t {
    constexpr UnaryFunction&& operator()(Tuple&& t, UnaryFunction&& f) const
    {
        std::forward<UnaryFunction>(f)(
                std::get<FromIndex::value>(std::forward<Tuple>(t)));
        return for_each_t<index_t<FromIndex::value + 1>,
                          ToIndex,
                          Tuple,
                          UnaryFunction>()(
                std::forward<Tuple>(t), std::forward<UnaryFunction>(f));
    }
};

// specialization for empty tuple-likes
template<typename FromIndex, typename Tuple, typename UnaryFunction>
struct for_each_t<FromIndex, index_t<0>, Tuple, UnaryFunction> {
    constexpr UnaryFunction&& operator()(Tuple&&, UnaryFunction&& f) const
    {
        return std::forward<UnaryFunction>(f);
    }
};

// specialization for last element
template<typename ToIndex, typename Tuple, typename UnaryFunction>
struct for_each_t<index_t<ToIndex::value - 1>, ToIndex, Tuple, UnaryFunction> {
    constexpr UnaryFunction&& operator()(Tuple&& t, UnaryFunction&& f) const
    {
        std::forward<UnaryFunction>(f)(
                std::get<ToIndex::value - 1>(std::forward<Tuple>(t)));
        return std::forward<UnaryFunction>(f);
    }
};

}  // namespace detail

template<typename Tuple, typename UnaryFunction>
constexpr UnaryFunction for_each(Tuple&& t, UnaryFunction&& f)
{
    return detail::for_each_t<detail::index_t<0>,
                              detail::index_t<std::tuple_size<
                                  std::remove_reference_t<Tuple>
                              >::value>,
                              Tuple,
                              UnaryFunction>()(
            std::forward<Tuple>(t), std::forward<UnaryFunction>(f));
}


int main(int argc, const char** argv)
{

    constexpr auto all = std::tuple_cat( foo_enumerator::reflect, bar_enumerator::reflect );

    for_each(all, [](auto e_value)  {
            std::cout << "Enumeration value: " << static_cast<unsigned int>(e_value) << std::endl;
    });
}

关于c++ - 遍历多个枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56865208/

相关文章:

C++11:我们什么时候需要为默认成员函数专门化 "=default"?

c++ - QTreeView可以加入QDockWidget吗

c++ - 如何使用 boost::asio:write 调用发送 ICU UnicodeString?

c++ - 我如何在 NAO v6 中使用 C++ 进行编程?

c++ - Armadillo 相当于 A(find(A<0)) = 0

c++ - MSVC10 Visual Studio 2010 是否支持基于 C++ 范围的循环

C++ - 通过传递对函数的引用来初始化变量?

c++ - 为什么我的 QML textArea 没有 append ?

c++ - 在不同线程中执行 future 时避免破坏对象

c++ - c++ 11 atomic是否自动解决变量读写上的多核竞争?