如何实现enum_limits? [英] How to implement enum_limits?

查看:61
本文介绍了如何实现enum_limits?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,

有没有人看到实现以下enum_limits模板的方法:


template< typename EnumType>

struct enum_limits {


// EnumType范围内的第一个元素:

static EnumType const min;


// EnumType范围内的最后一个元素:

static EnumType const max;


}; // enum_limits

这个想法是给定的


enum xxx {A,B,C};


代码


enum_limits< xxx> :: min


应该在编译时产生A的值。到目前为止,我必须为每个我需要enum_limits的枚举类型提供

特化。我会喜欢

抛弃它并用统一的黑客替换它。


任何想法?


Best


Kai-Uwe Bux

Hi folks,
does anyone see a way to implement the following enum_limits template:

template < typename EnumType >
struct enum_limits {

// first element in range of EnumType:
static EnumType const min;

// last element in range of EnumType:
static EnumType const max;

}; // enum_limits
The idea is that given

enum xxx { A, B, C };

the code

enum_limits<xxx>::min

should yield the value of A at compile time. So far, I have to provide a
specialization for each enum type that I need enum_limits for. I would love
to ditch that and replace it by a uniform hack.

Any ideas?

Best

Kai-Uwe Bux

推荐答案

Kai-Uwe Bux写道:
Kai-Uwe Bux wrote:
嗨大家好,

有没有人看到实现以下enum_limits模板的方法:

template< typename EnumType>
struct enum_limits {

// EnumType范围内的第一个元素:
静态EnumType const min;

//最后一个元素EnumType范围:
静态EnumType const max;

}; // enum_limits

这个想法是给定的

enum xxx {A,B,C};

代码

enum_limits< xxx> :: min

应该在编译时产生A的值。到目前为止,我必须为每个我需要enum_limits的枚举类型提供
特化。我很乐意放弃它并用统一的黑客替换它。

任何想法?
Hi folks,
does anyone see a way to implement the following enum_limits template:

template < typename EnumType >
struct enum_limits {

// first element in range of EnumType:
static EnumType const min;

// last element in range of EnumType:
static EnumType const max;

}; // enum_limits
The idea is that given

enum xxx { A, B, C };

the code

enum_limits<xxx>::min

should yield the value of A at compile time. So far, I have to provide a
specialization for each enum type that I need enum_limits for. I would love
to ditch that and replace it by a uniform hack.

Any ideas?




不是吗只是更容易让枚举本身用

声明最小值和最大值?标准的命名约定会使他们的名字可以预测。也就是说,而不是:


enum xxx {A,B,C};


将枚举枚举添加到括号中的括号:


enum xxx {xxxMin = -1,A,B,C,xxxMax};


模板方法遇到枚举的问题不是b $ b可数或可迭代的。但正是因为枚举不能被计算或以系统的方式迭代,所以在枚举中添加新的枚举器

总是安全的,而不会有任何破坏的风险(如只要

的值保留现有的枚举);所以我建议将

哨兵值添加到枚举声明本身。


这种方法的另一个优点是易于维护。添加或

从枚举中删除枚举会自动更新Min和

最大哨兵。


Greg



Wouldn''t it just be easier to have the min and max values declared with
the enum itself? A standard naming convention would make their names
predictable. That is, instead of:

enum xxx {A, B, C};

add sentinel enums to the bracket the enumeration:

enum xxx { xxxMin = -1, A, B, C, xxxMax };

The template approach runs into the problem that enums are not
countable or iterable. But precisely because enums cannot be counted or
iterated in a systematic way, it''s always safe to add new enumerators
to an enum without risk of breaking anything (as long as the values of
the existing enumerations are preserved); so I recommend adding
sentinel values to the enum declarations themselves.

A further advantage of this approach is ease-of maintainence. Adding or
removing enumerations from an enum automatically updates the Min and
Max sentinels.

Greg


Greg写道:
Greg wrote:
Kai-Uwe Bux写道:
Kai-Uwe Bux wrote:
嗨大家好,

有没有人看到实现以下enum_limits模板的方法:

template< typename EnumType>
struct enum_limits {

// EnumType范围内的第一个元素:
静态EnumType const min;

//最后一个元素EnumType范围:
静态EnumType const max;

}; // enum_limits

这个想法是给定的

enum xxx {A,B,C};

代码

enum_limits< xxx> :: min

应该在编译时产生A的值。到目前为止,我必须为每个我需要enum_limits的枚举类型提供
特化。我愿意放弃它并用统一的黑客替换它。

任何想法?
用最小值和最大值声明是不是更容易
枚举本身?标准的命名约定会使他们的名字可以预测。也就是说,而不是:

枚举xxx {A,B,C};

将哨兵枚举添加到括号中:枚举:

枚举xxx {xxxMin = -1,A,B,C,xxxMax};
Hi folks,
does anyone see a way to implement the following enum_limits template:

template < typename EnumType >
struct enum_limits {

// first element in range of EnumType:
static EnumType const min;

// last element in range of EnumType:
static EnumType const max;

}; // enum_limits
The idea is that given

enum xxx { A, B, C };

the code

enum_limits<xxx>::min

should yield the value of A at compile time. So far, I have to provide a
specialization for each enum type that I need enum_limits for. I would
love to ditch that and replace it by a uniform hack.

Any ideas?
Wouldn''t it just be easier to have the min and max values declared with
the enum itself? A standard naming convention would make their names
predictable. That is, instead of:

enum xxx {A, B, C};

add sentinel enums to the bracket the enumeration:

enum xxx { xxxMin = -1, A, B, C, xxxMax };




它会更容易,但它不能解决由
我的问题。我还有另一个模板


模板< typename EnumType>

struct什么{

...

};


及其内我需要知道的任何模板EnumType的最小值和最大值。

所以我需要一个模板解决方案 - 名字的可预测性不好

足够。


模板方法会遇到枚举不可数或可迭代的问题。但正是因为枚举不能被系统地计算或重复,所以将新的枚举器添加到枚举中总是安全的,而没有破坏任何东西的风险(只要
保留现有的枚举);所以我建议将
sentinel值添加到枚举声明本身。

这种方法的另一个优点是易于维护。添加或删除枚举中的枚举会自动更新Min和
Max哨兵。



It would be much easier, but it would not solve the problem that prompted by
my question. I have yet another template

template < typename EnumType >
struct whatever {
...
};

and within the whatever template I need to know the min and max of EnumType.
So I need a templated solution -- predictability of names is not good
enough.

The template approach runs into the problem that enums are not
countable or iterable. But precisely because enums cannot be counted or
iterated in a systematic way, it''s always safe to add new enumerators
to an enum without risk of breaking anything (as long as the values of
the existing enumerations are preserved); so I recommend adding
sentinel values to the enum declarations themselves.

A further advantage of this approach is ease-of maintainence. Adding or
removing enumerations from an enum automatically updates the Min and
Max sentinels.




好​​吧,如果有一个统一的enum_limits实现模板,它

会透明地工作,并且需要客户的*不小心。


谢谢


Kai-Uwe Bux



Well, if there was a uniform implementation for the enum_limits template, it
would work transparently and require *no* care on the part of the client.

Thanks

Kai-Uwe Bux


Kai-Uwe Bux写道:
Kai-Uwe Bux wrote:
嗨伙计们,

有没有人看到实现以下enum_limits模板的方法:

template< typename EnumType>
struct enum_limits {

// EnumType范围内的第一个元素:
静态EnumType const min;

//最后一个元素EnumType范围:
静态EnumType const max;

}; // enum_limits

这个想法是给定的

enum xxx {A,B,C};

代码

enum_limits< xxx> :: min

应该在编译时产生A的值。到目前为止,我必须为每个我需要enum_limits的枚举类型提供
特化。我很乐意放弃它并用统一的黑客替换它。

任何想法?

最好的

Kai-Uwe Bux
Hi folks,
does anyone see a way to implement the following enum_limits template:

template < typename EnumType >
struct enum_limits {

// first element in range of EnumType:
static EnumType const min;

// last element in range of EnumType:
static EnumType const max;

}; // enum_limits
The idea is that given

enum xxx { A, B, C };

the code

enum_limits<xxx>::min

should yield the value of A at compile time. So far, I have to provide a
specialization for each enum type that I need enum_limits for. I would love
to ditch that and replace it by a uniform hack.

Any ideas?

Best

Kai-Uwe Bux




没有模板的想法。我见过:


enum设备

{

MIN_DEVICE = 0,

键盘= MIN,

MOUSE = 1,

// ...

PRINTER = 100,

MAX_DEVICE =打印机

};


它仍然需要额外的打字和维护,但至少如果

程序员让它们更新,s /他可以做类似的事情:


int i = // ...

断言(i> = MIN_DEVICE&& i< = MAX_DEVICE);


这个解决方案将所有代码放在一个地方(不像模板

想法),这可能会使维护更容易,但它也需要

非统一命名(MIN_DEVICE而不是enum_limits< xxx> :: min)。


在CUJ的网站上快速搜索其他几个您可能感兴趣的可能性



http://www.cuj.com/documents/s=8205/...d/web0305d.htm
http://www.cuj.com/documents/s=8054/...ker/walker.htm
http://www.cuj .com / documents / s = 8470 / ... ser / besser.htm


干杯! --M



No template ideas. I''ve seen:

enum Devices
{
MIN_DEVICE = 0,
KEYBOARD = MIN,
MOUSE = 1,
// ...
PRINTER = 100,
MAX_DEVICE = PRINTER
};

It still requires extra typing and maintenance, but at least if the
programmer keeps them updated, s/he can do something like:

int i = //...
assert( i >= MIN_DEVICE && i <= MAX_DEVICE );

This solution locates all the code in one place (unlike the template
idea), which might make maintenance easier, but it also requires
non-uniform naming (MIN_DEVICE instead of enum_limits<xxx>::min).

A quick search on CUJ''s website turned up a couple other possibilities
that you might be interested in:

http://www.cuj.com/documents/s=8205/...d/web0305d.htm
http://www.cuj.com/documents/s=8054/...ker/walker.htm
http://www.cuj.com/documents/s=8470/...ser/besser.htm

Cheers! --M


这篇关于如何实现enum_limits?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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