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

发布于 2024-12-17 12:29:12 字数 1618 浏览 2 评论 0原文

基于这个问题:

“枚举类”模拟或实体MSVC 10.0 的替代方案

我想问几件事。假设这段代码:

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;
};

以及它们的后续定义:

    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);

人们可以这样使用:

    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;
        }
    }

为什么调用函数调用运算符?如何手动调用它来解决 case 语句中的“问题”?我正在使用 MSVS 2008 没有可用的外部库。

Based on this question:

"enum class" emulation or solid alternative for 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? I am using MSVS 2008 no external libs are available.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念绕指尖 2024-12-24 12:29:12

这里没有声明函数调用运算符。

operator int const() const { return myVal; }

是一个用户定义的转换,它将 DeletionMode_E 类型的对象转换为常量整数。要调用它,您必须执行强制转换(这是在 switch 语句中隐式完成的)。

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. To invoke it, you have to perform a cast (this is done implicitly in your switch-statement).

过去的过去 2024-12-24 12:29:12

在 C++03 中,函数调用不能发生在常量表达式(例如 case 标签)中,因此这是不行的。在 C++11 中,您只需将转换函数和构造函数标记为 constexpr

我不确定“函数调用运算符”是什么意思。如果您指的是函数operator int const()(转换函数),则调用它是因为switch语句需要一个整型表达式,因此转换函数用于执行转换——这就是他们所做的。顺便说一句,第一个 const 是无用的,它应该是operator int() const { return myVal; }。要手动调用它,只需使用通常语法中的名称:delMode_in.operator int const()

_E 后缀似乎表示对枚举某些方面的模拟。为什么不直接使用枚举

In C++03, a function call cannot occur in a constant expression (such as a case label), so it's a no-go. In C++11, you would simply have to mark the conversion function and constructor as 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. Incidentally, the first const is useless and it should be operator int() const { return myVal; }. To call it manually, just use its name in the usual syntax: delMode_in.operator int const().

The _E suffix seems to indicate emulation of some aspects of enumerations. Why not just use an enum?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文