gpt4 book ai didi

c++ - Enum 不是一个 constexpr?

转载 作者:行者123 更新时间:2023-11-30 03:29:13 25 4
gpt4 key购买 nike

我有这样一个源代码,有一个枚举,我希望它可以被评估为 constexpr,但编译器给我一个错误,它不是。为什么?EventOrder 是 enum 还是 enum class 并不重要。

#include <limits>
#include <type_traits>

enum class EventOrder
{
Last = 1'000'000,
Default = 0,
Logger = -1024,
First = -1'000'000
};

template <typename T>
constexpr inline std::underlying_type_t<T> num(T value) {
static_assert(std::is_enum<T>::value, "num can only be used on enumeration types");
return static_cast<std::underlying_type_t<T>>(value);
}

constexpr EventOrder sum(const EventOrder order, const std::underlying_type_t<EventOrder> orderDelta)
{
static_assert(order >= EventOrder::First, "Order Value out of bounds");
return static_cast<EventOrder>(num(order) + orderDelta);
}

int main( )
{
constexpr EventOrder e = EventOrder::Default;
sum(e, 2);
return 0;
}

它给出了错误:

    $ g++ -std=c++14 EventTest.cc
EventTest.cc: In function ‘constexpr EventOrder sum(EventOrder, std::underlying_type_t<EventOrder>)’:
EventTest.cc:23:2: error: non-constant condition for static assertion
static_assert(order >= EventOrder::First, "Order Value out of bounds");
^
EventTest.cc:23:2: error: ‘order’ is not a constant expression

为什么 order 不是 constexpr?

编辑 1

那么将参数作为模板变量传递是解决该问题的唯一方法吗?或者你知道一些不同的方法?

#include <limits>
#include <type_traits>

enum class EventOrder
{
Last = 1'000'000,
Default = 0,
Logger = -1024,
First = -1'000'000
};

template <typename T> constexpr inline std::underlying_type_t<T> num(T value)
{
static_assert(std::is_enum<T>::value, "num can only be used on enumeration types");
return static_cast<std::underlying_type_t<T>>(value);
}

template< typename T >
constexpr bool LimitedValue(const T value, const T min, const T max)
{
return value >= min && value <= max;
}

template <EventOrder orderl, std::underlying_type_t<EventOrder> orderr>
constexpr std::underlying_type_t<EventOrder> orderSum()
{
return num(orderl) + orderr;
}

template <EventOrder orderl, std::underlying_type_t<EventOrder> orderr>
constexpr EventOrder order()
{
static_assert(LimitedValue(orderSum<orderl, orderr>(), num(EventOrder::First), num(EventOrder::Last)), "order out of baunds");
return static_cast<EventOrder>(orderSum<orderl, orderr>());
}

int main()
{
EventOrder e = order<EventOrder::Default, 2>();

}

最佳答案

即使该函数是一个 constexpr 函数,它仍然可以使用非 const 参数调用。因此,当编译器处理该函数时,它无法知道 order 的值,也无法在 static_assert 中使用它。

关于c++ - Enum 不是一个 constexpr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45875323/

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