简体   繁体   English

等效于C ++枚举-转换为常量表达式

[英]C++ enum equivalent - convert to constant expression

Based on this question: 基于此问题:

"enum class" emulation or solid alternative for MSVC 10.0 MSVC 10.0的“枚举类”仿真或替代

I would like to ask a couple of things. 我想问几件事。 Assuming this code: 假设此代码:

struct DeletionMode_E
{
    static DeletionMode_E const Off;
    static DeletionMode_E const DirSize;
    static DeletionMode_E const FileNumberSize;
    static DeletionMode_E const DirAndFileNumberSize;

    operator int const() const { return myVal; }

private:
    explicit DeletionMode_E(const int & v) : myVal(v) { }
    const int myVal;
};

And their subsequent definitions : 及其后续定义:

    Log4Reconstruction::DeletionMode_E const Log4Reconstruction::DeletionMode_E::Off(0);
    Log4Reconstruction::DeletionMode_E const Log4Reconstruction::DeletionMode_E::DirSize(1);
    Log4Reconstruction::DeletionMode_E const Log4Reconstruction::DeletionMode_E::FileNumberSize(2);
    Log4Reconstruction::DeletionMode_E const Log4Reconstruction::DeletionMode_E::DirAndFileNumberSize(3);

One can use this like : 一个人可以这样使用:

    void Log4Reconstruction::setDeletionMode( Log4Reconstruction::DeletionMode_E const & delMode_in)
    {
        std::cout << delMode_in << std::endl;

        switch(delMode_in)
        {
        //case Log4Reconstruction::DeletionMode_E::Off: C2051 case expression not constant
        //  std::cout << "Off" << std::endl;
        //  break;
        case 1:
            std::cout << "File number" << std::endl;
            break;
        }
    }

Why is the function call operator called? 为什么调用函数调用运算符? How would one call it manually in order to solve the "problem" in the case statement? 一个人将如何手动调用它以解决case语句中的“问题”? I am using MSVS 2008 no external libs are available. 我正在使用MSVS 2008, 没有可用的外部库。

There is no function-call operator declared here. 这里没有声明函数调用运算符。

operator int const() const { return myVal; }

Is a user-defined conversion which converts an object of type DeletionMode_E to a constant integer. 是用户定义的转换,它将DeletionMode_E类型的对象转换为常数整数。 To invoke it, you have to perform a cast (this is done implicitly in your switch -statement). 要调用它,您必须执行强制switch (在switch语句中隐式完成)。

In C++03, a function call cannot occur in a constant expression (such as a case label), so it's a no-go. 在C ++ 03中,函数调用不能在常量表达式(例如case标签)中发生,因此是不可行的。 In C++11, you would simply have to mark the conversion function and constructor as constexpr . 在C ++ 11中,您只需要将转换函数和构造函数标记为constexpr

I'm not sure what you mean by "function call operator." 我不确定“函数调用运算符”的含义。 If you mean the function operator int const() (the conversion function), it is called because the switch statement expects an integral expression, so the conversion function is used to perform the conversion — that's what they do. 如果您指的是函数operator int const() (转换函数),则会调用它,因为switch语句需要一个整数表达式,因此转换函数用于执行转换-这就是它们的作用。 Incidentally, the first const is useless and it should be operator int() const { return myVal; } 顺便说一句,第一个const是无用的,它应该是operator int() const { return myVal; } operator int() const { return myVal; } . operator int() const { return myVal; } To call it manually, just use its name in the usual syntax: delMode_in.operator int const() . 要手动调用它,只需使用通常的语法使用其名称: delMode_in.operator int const()

The _E suffix seems to indicate emulation of some aspects of enumerations. _E后缀似乎表明对枚举的某些方面进行了仿真。 Why not just use an enum ? 为什么不只使用enum

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

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