更智能的枚举 [英] smarter enums

查看:74
本文介绍了更智能的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在努力让我的枚举变得更聪明 - 也就是说,更多

与其他语言一致。我还没有测试过,但是b $ bi想出的是这样的模板:


模板< typename枚举>

class smart_enum

{

public:

typedef Enum enum_type;


smart_enum (){}

smart_enum(enum_type e)e​​_(e){}

enum_type get()const {return e_; }


bool运算符==(smart_enum const& e)const {return e.e_ == e_; }

bool operator!=(smart_enum const& e)const {return!(e_ == * this); } $ />

运算符enum_type()const {return e_; }


私人:

enum_type e_;

};


i想要这用于透明地替换现有的枚举

类型 - 有一个例外。我想不允许:


颜色c =绿色;


和强制:


颜色c = color :: green;


否则,我希望模板的行为与常规枚举完全相同

类型。这个模板会起作用吗?或者我错过了什么?


我正在考虑的下一件事就是过渡到智能

枚举简单,使用宏。类似这样的事情:


#define DEFINE_ENUM_START(enum_t)\

名称空间enum_t ## _DUMB_ENUM_ {\

typedef enum

#define DEFINE_ENUM_END(enum_t)\

enum_t; } \

typedef smart_enum< enum_t ## _DUMB_ENUM_ :: enum_t> enum_t;


再次,未测试甚至编译。但我想能做的是:


DEFINE_ENUM_START(颜色)

{

红色,

绿色,

蓝色

}

DEFINE_ENUM_END(颜色)


有没有人见过这样的事情呢?

这样做有什么问题吗?


mark


i have been toying with the idea of making my enums smarter - ie, more
in line with the rest of the language. i haven''t tested it yet, but what
i came up with is a template like this:

template <typename Enum>
class smart_enum
{
public:
typedef Enum enum_type;

smart_enum() {}
smart_enum(enum_type e) e_(e) {}

enum_type get() const { return e_; }

bool operator==(smart_enum const& e) const { return e.e_ == e_; }
bool operator!=(smart_enum const& e) const { return !(e_ == *this); }

operator enum_type() const { return e_; }

private:
enum_type e_;
};

i would like this to be used to transparently replace existing enum
types - with one exception. i want to disallow:

colour c = green;

and force:

colour c = colour::green;

otherwise, i want the template to behave exactly like a regular enum
type. would this template work? or am i missing anything?

the next thing i am thinking about is making the transition to smart
enums easy, using macros. something like this:

#define DEFINE_ENUM_START(enum_t) \
namespace enum_t ## _DUMB_ENUM_ { \
typedef enum
#define DEFINE_ENUM_END(enum_t) \
enum_t; } \
typedef smart_enum< enum_t ## _DUMB_ENUM_ :: enum_t > enum_t;

again, not tested or even compiled. but what i''d like to be able to do is:

DEFINE_ENUM_START(colour)
{
red,
green,
blue
}
DEFINE_ENUM_END(colour)

has anyone seen anything like this before? are there any problems with
doing this?

mark

推荐答案

" Mark A. Gibbs" < X _ ********* @ rogesr.com_x>写了...
"Mark A. Gibbs" <x_*********@rogesr.com_x> wrote...

我一直在努力让我的枚举变得更聪明 - 也就是说,更符合其他语言。我还没有测试过,但是我想出的是这样的模板:

模板< typename枚举>
类smart_enum
{
public:
typedef enum enum_type;

smart_enum(){}
smart_enum(enum_type e)e​​_(e){}

enum_type get()const {return e_; }

bool operator ==(smart_enum const& e)const {return e.e_ == e_; }
bool operator!=(smart_enum const& e)const {return!(e_ == * this); }运算符enum_type()const {return e_; }

私人:
enum_type e_;
};

我希望这可以用来透明地替换现有的枚举类型 - 除了一个例外。我想不允许:

颜色c =绿色;

并强制:

颜色c =颜色::绿色;

否则,我希望模板的行为与常规枚举类型完全相同。这个模板会起作用吗?或者我错过了什么?

我正在考虑的下一件事是使用宏来轻松过渡到智能
枚举。类似这样的事情:

#define DEFINE_ENUM_START(enum_t)\
命名空间enum_t ## _DUMB_ENUM_ {\
typedef enum
#define DEFINE_ENUM_END(enum_t)\
enum_t; } \
typedef smart_enum< enum_t ## _DUMB_ENUM_ :: enum_t> enum_t;

再次,未测试甚至编译。但我想能做的是:

DEFINE_ENUM_START(颜色)
{
红色,
绿色,
蓝色
}
DEFINE_ENUM_END(颜色)

以前有人见过这样的东西吗?
这样做有什么问题吗?

i have been toying with the idea of making my enums smarter - ie, more
in line with the rest of the language. i haven''t tested it yet, but what
i came up with is a template like this:

template <typename Enum>
class smart_enum
{
public:
typedef Enum enum_type;

smart_enum() {}
smart_enum(enum_type e) e_(e) {}

enum_type get() const { return e_; }

bool operator==(smart_enum const& e) const { return e.e_ == e_; }
bool operator!=(smart_enum const& e) const { return !(e_ == *this); }

operator enum_type() const { return e_; }

private:
enum_type e_;
};

i would like this to be used to transparently replace existing enum
types - with one exception. i want to disallow:

colour c = green;

and force:

colour c = colour::green;

otherwise, i want the template to behave exactly like a regular enum
type. would this template work? or am i missing anything?

the next thing i am thinking about is making the transition to smart
enums easy, using macros. something like this:

#define DEFINE_ENUM_START(enum_t) \
namespace enum_t ## _DUMB_ENUM_ { \
typedef enum
#define DEFINE_ENUM_END(enum_t) \
enum_t; } \
typedef smart_enum< enum_t ## _DUMB_ENUM_ :: enum_t > enum_t;

again, not tested or even compiled. but what i''d like to be able to do is:

DEFINE_ENUM_START(colour)
{
red,
green,
blue
}
DEFINE_ENUM_END(colour)

has anyone seen anything like this before? are there any problems with
doing this?




你的智能枚举怎么样?开始在开关工作声明?


我仍​​在努力了解你的枚举有什么好处已经超过

现有的枚举机制?究竟是什么意思更多的是&

与其他语言一致?你能为我这样的假人演示

吗?


V


P.S.对不起,报价/增加的比例很差,只想保留所有

的原帖



How are your "smart enums" going to work in a "switch" statement?

I am still trying to understand what advantage your "enum" has over
the existing enum mechanism? What exactly do you mean by "more in
line with the rest of the language"? Could you please demonstrate
for dummies like me?

V

P.S. Sorry for the bad quoted/added ratio, just wanted to keep all
of the original post




Victor Bazarov写道:

Victor Bazarov wrote:
你的智能枚举怎么样?开始在开关工作声明?


我不知道,这是我没有想到的事情之一。隐式转换运算符不会这么做吗?

我仍​​然试图理解你的enum有什么优势?已经结束了现有的枚举机制?究竟是什么意思更多与
符合其他语言?你能否像我一样展示
How are your "smart enums" going to work in a "switch" statement?
i don''t know, that was one of those things i hadn''t thought of. wouldn''t
the implicit conversion operator do the trick?
I am still trying to understand what advantage your "enum" has over
the existing enum mechanism? What exactly do you mean by "more in
line with the rest of the language"? Could you please demonstrate
for dummies like me?




主要是为了防止枚举成员名字被注入

周围范围。但是你可以添加很多功能,例如默认值
,或者在比较时可能会做更多的工作,

分配,或者其他什么。 />

就在我的脑海中,考虑颜色枚举:


namespace colour_ENUM_ {

typedef enum

{

红色,

绿色,

蓝色

}颜色;

}


模板<>

类smart_enum< colour_ENUM _ :: color>

{

public:

typedef Enum colour_ENUM _ :: color;


smart_enum(){}

smart_enum( enum_type e)e​​_(e){}


enum_type get()const {return e_; }


bool运算符==(smart_enum const& e)const {return e.e_ == e_; }

bool operator!=(smart_enum const& e)const {return!(e_ == * this); } $ />

运算符enum_type()const {return e_; }


//额外的东西

string get_colour_name()const {

const char * str;


开关(e_)

{

案例红色:str =" red" ;;

case green:str = green;

case blue:str =" blue";

}


return string(str);

}


long get_html_colour()const {

switch(e_)

{

case red:返回0xFF0000L;

case green:return 0x00FF00L;

case blue:return 0x0000FFL;

} < br $>
}


私人:

enum_type e_;

};

使用旧颜色枚举的现有代码应该保持不变

(除了必须将范围添加到常量中),但新代码

可以使用新成员以更有用的格式获得颜色

输出。


可能还有其他的东西 - 我一直在沉思这对于现在几天的b $ b,但现在我必须跑。如果你能坚持3天左右的时间,我可以回复你。否则,那个

是我最近在c / c ++用户中重读的一篇文章(我没有和我在一起,而且b $ b不在线) 's journal - 由hyslop&

sutter的其中一个。也许是2004年4月?如果没有,可能可能。是什么让我想到这些线路上的
。那里可能还有更多比我能来的更多




但是一个很好的总结是这是关于维护源

向后兼容性(在某种程度上,枚举的语法),而

将枚举类型提升为一流的c ++类型。


mark


ps你知道,把自己称为假人是一种自尊心很差的表现,但除此之外,如果你投射出一个假人的自我形象,

人我们几乎不会劝阻你的意见,

并最终可能会默认同意。我不是一个真正的精神科医生,但我只是在电视上看到了一个。



mostly it prevents the enum member names from being injected into the
surrounding scope. but there''s a lot of functionality you could add,
such as a default value, or maybe doing some more work while comparing,
assigning, or whatever.

just off the top of my head, consider the colour enumeration:

namespace colour_ENUM_ {
typedef enum
{
red,
green,
blue
} colour;
}

template <>
class smart_enum<colour_ENUM_::colour>
{
public:
typedef Enum colour_ENUM_::colour;

smart_enum() {}
smart_enum(enum_type e) e_(e) {}

enum_type get() const { return e_; }

bool operator==(smart_enum const& e) const { return e.e_ == e_; }
bool operator!=(smart_enum const& e) const { return !(e_ == *this); }

operator enum_type() const { return e_; }

// additional stuff
string get_colour_name() const {
const char* str;

switch(e_)
{
case red: str = "red";
case green: str = "green";
case blue: str = "blue";
}

return string(str);
}

long get_html_colour() const {
switch(e_)
{
case red: return 0xFF0000L;
case green: return 0x00FF00L;
case blue: return 0x0000FFL;
}
}

private:
enum_type e_;
};

existing code that used the old colour enum should work unchanged
(except that the scope must be added to the constants), but new code
could use the new members to get the colour in a format more useful for
output.

there are probably other things too - i''ve been musing over this for a
couple days now, but right now i have to run. if you can hang on for
about 3 days, i can probably get back to you with more. otherwise, there
was an article that i re-read recently (which i don''t have with me and
isn''t online) in c/c++ user''s journal - one of the ones by hyslop &
sutter. april 2004, maybe? if not, probably may. that was what got me to
thinking on these lines. there might be more in there than i can come up
with.

but a good summary is that this is about maintaining source
backwards-compatibility (and to some degree, the syntax of enums), while
elevating enum types to first-class c++ types.

mark

p.s. you know, calling yourself a dummy is a sign of poor self-esteem,
but aside from that, if you project a self-image of being a dummy,
people will hardly go out of their way to dissuade you of your opinion,
and may end up agreeing by default. i''m not really a psychiatrist, but i
just saw one on tv.


" Mark A. Gibbs" ; < X _ ********* @ rogesr.com_x>写了...
"Mark A. Gibbs" <x_*********@rogesr.com_x> wrote...

Victor Bazarov写道:

Victor Bazarov wrote:
你的智能枚举怎么样?开始在开关工作声明?
How are your "smart enums" going to work in a "switch" statement?



我不知道,这是我没有想到的事情之一。不会隐含转换运算符吗?



i don''t know, that was one of those things i hadn''t thought of. wouldn''t
the implicit conversion operator do the trick?

我仍然试图理解你的枚举有什么好处?已经结束了现有的枚举机制?究竟是什么意思更多与
符合其他语言?你能否为我这样的假人演示
I am still trying to understand what advantage your "enum" has over
the existing enum mechanism? What exactly do you mean by "more in
line with the rest of the language"? Could you please demonstrate
for dummies like me?



主要是它会阻止枚举成员名称[...]



mostly it prevents the enum member names [...]




感谢您的解释和心理(而不是精神病学)

课程(你知道区别吗?我可能不会,我只是假装

我做)。但是,我只是想指出你可能已经用错误的前提开始了

。 Enums没有会员。


祝你好运!


Victor



Thank you for your explanation and your psychology (and not psychiatry)
lesson (do you know the difference? I probably don''t, I only pretend
I do). However, I just wanted to point out that you may have started
with a wrong premise. Enums don''t have members.

Best of luck!

Victor


这篇关于更智能的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆