在多个枚举上循环
looping over multiple enums
我将如何在多个枚举上循环。
我有点想在骨架内有枚举
不确定如何构造我的枚举以将它们放在这样的容器中
那么不确定我如何循环以获取每种类型
我得到了基于元组的元编程方法
#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 是否会采用反射,无论是以这种形式还是另一种形式。
对不起,如果不是您想要的全部答案。
相关文章:
- 不带大括号的枚举形式
- 枚举环境变量的惯用C++14/C++17方法
- 类似枚举的计算常量
- 如何正确实现和访问运算符的各种自定义枚举器
- 错误:从"int"到枚举c++的转换无效
- C++中构造函数中的枚举
- 在多个枚举上循环
- 枚举不命名类型(无循环依赖关系)
- 循环通过常量枚举#define
- C++中类似 Python 的循环枚举
- 如何使用枚举类值作为 for 循环的一部分
- 你能使用 Qt foreach 循环迭代每个可能的枚举值吗?
- 由于嵌套了枚举类,C++循环依赖关系
- C++使用 for 循环的枚举来填充浮点数组
- c++枚举问题和c++中可能的循环依赖
- 从整数索引的for循环枚举到range-for
- 不带循环迭代器的枚举
- 用于枚举完整图的哈密顿循环的算法(循环、反向、环绕或重复不计算在内的排列)
- 向后循环遍历枚举值
- 在c++中,由于类中的枚举,循环包含