【问题标题】:nonspecific enum function parameter非特定枚举函数参数
【发布时间】:2014-10-15 23:27:51
【问题描述】:

我正在尝试编写一个将枚举作为参数的函数,就像函数将采用 int 或 bool 等一样。

例如,我使用的是 c++ void Func (enum Blah, int blah);

知道如何让函数将非特定枚举作为参数吗?

我知道我可以将枚举转换为整数并像这样传递它们

枚举 foo{BAR};

void Func (int myenum, int blah); 函数 ((int)BAR, 0) 但是我不想通过枚举以外的任何东西。

我听说过仿制药,我不确定它们是否是答案,因为我找不到任何关于它们的确切信息。

因此,非常感谢一些可以帮助我的代码或链接。提前致谢。

【问题讨论】:

标签: c++ function generics parameters enums


【解决方案1】:
template <typename Enum>
void Func (Enum blah, int fleh)
{
  static_assert(std::is_enum<Enum>::value, "Only enums are allowed here");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多