在多个枚举上循环

looping over multiple enums

本文关键字:循环 枚举      更新时间:2023-10-16

我将如何在多个枚举上循环。

我有点想在骨架内有枚举

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

那么不确定我如何循环以获取每种类型

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

#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 (™(中。

要做您描述的事情,您需要反思。反思使您可以在运行时以动态的方式浏览对象。

c#/。网络提供了这一点。这是C#。

中的一个示例
enum MyEnum { Test, Test1, Test2, Another, One, Bites, The, Dust }
foreach (var value in typeof(MyEnum).GetEnumValues()) {
    WriteLine(value.ToString());
}

我不确定C 是否会采用反射,无论是以这种形式还是另一种形式。

对不起,如果不是您想要的全部答案。