【发布时间】:2021-02-21 20:56:07
【问题描述】:
我想使用enum class <NAME> 作为位掩码。我已经编写了以下概念,限制 only Enum 类的模板使用:
template <class E>
concept Enumeration =
std::is_enum_v<E> &&
!std::convertible_to<E, std::underlying_type<E>>;
现在使用这个概念来制作一个添加模板:
template <Enumeration Element>
inline constexpr Element operator + (Element lhs, Element rhs) {
using T = std::underlying_type_t<Element>;
return static_cast<Element>(static_cast<T>(lhs) | static_cast<T>(rhs));
}
但这会污染所有带有位掩码运算符的enum classes。当枚举类定义如下时,我想限制使用:
enum class An_Enum : Bitmask_t<std::size_t> {
};
Bitmask_t 是这样的受限模板:
template <class T>
concept Bitmask_t = std::integral<T>;
如何在我的枚举概念中强制执行此限制?有没有更好的方法来实现这个模板约束?
【问题讨论】:
标签: c++ templates enums c++20 c++-concepts