简体   繁体   English

在 C++,如何将模板参数强制为范围枚举值类型?

[英]in C++, How to enforce template parameter to be of a scoped enum value type?

I have a class template that I'd like to write as follows:我有一个 class 模板,我想这样写:

template </*what to put here?*/ T>
Class Bar {};

I would like to enforce T to only be a value from a scoped enum.我想强制 T 只是来自范围枚举的值。 I used the is_scoped_enum type check provided here , however the best I was able to come up with was to change Bar to be like this:我使用了此处提供的is_scoped_enum类型检查,但是我能想到的最好办法是将Bar更改为如下所示:

template <typename T>
concept ScopeEnum = is_scoped_enum<T>::value;

template<ScopeEnum SE, SE se>
class Bar {};

How can I implement it so Bar remains as intended?我如何实施它才能使Bar保持原样?

Use a generic non-type parameter, and constrain its declaration使用泛型非类型参数,并约束其声明

template <ScopedEnum auto se>
class Bar {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM