C++枚举等效项 - 转换为常量表达式
基于这个问题:
我想问几件事。假设这段代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里没有声明函数调用运算符。
是一个用户定义的转换,它将
DeletionMode_E
类型的对象转换为常量整数。要调用它,您必须执行强制转换(这是在switch
语句中隐式完成的)。There is no function-call operator declared here.
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 yourswitch
-statement).在 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 theswitch
statement expects an integral expression, so the conversion function is used to perform the conversion — that's what they do. Incidentally, the firstconst
is useless and it should beoperator 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 anenum
?