English 中文(简体)
这一列举是否有效,如果是,为什么?
原标题:Is this enumeration valid, and if so, why?
  • 时间:2012-04-09 14:19:16
  •  标签:
  • c++
enum color = {blue, black=3, yellow=3};

2个颜色具有价值3,是否有效? 我认为,列举必须具有不同的价值观。

最佳回答

它有效。 也许不是好的设计。

就原因而言,我不肯定你在寻找答案。 如果不允许这样做,那么,它就会防止出现两种含义所指的相同价值的情况。 (我确信,我可以很容易地举出这样的例子。)

因此,如果它选择限制我可以做些什么,或者由于我通常会赢得希望重复的话而受到限制,那么我会以这种方式投票。

问题回答

The C++ standard, section 7.2, part 1, requires only that the constant expression be of an integral or enumerated type; there is no requirement for the constant values to be distinct. This gives you additional flexibility at aliasing your constants, if you think it makes your code more expressive. For example,

enum color {red=1,green=2,blue=3,max_color=3};

if (myColor > max_color) {/* report an error *}

优于

enum color {red=1,green=2,blue=3};

if (myColor > blue) {/* report an error *}

认为你制定了框架。 这一框架使用参数

出于某种原因,你对较早使用的术语感到不快。

仅仅替换这一术语就会打破现有的软件。 您决定提供新旧术语(至少一个释放周期)

是的,是有效的。 因为它不违反语言光谱。 The following isquoted from draft N3242, as You can see in example, Value associated with different listor need not be separate:

The identifiers in an enumerator-list are declared as constants, 
and can appear wherever constants are required. An enumeratordefinition
with = gives the associated enumerator the value indicated by the 
constant-expression. The constant-expression shall be an integral 
constant expression (5.19). If the first enumerator has no initializer,
the value of the corresponding constant is zero. An enumerator-definition without
an initializer gives the enumerator the value obtained by
increasing the value of the previous enumerator by one.
[ Example:
enum { a, b, c=0 };
enum { d, e, f=e+2 };
defines a, c, and d to be zero, b and e to be 1, and f to be 3. —end example ]
#include <iostream>

using namespace std;

enum color {blue, black=3, yellow=3};

int main()
{

    color a = blue;
    color b = black;
    color c = yellow;
    cout<<a<<endl;
    cout<<b<<endl;
    cout<<c<<endl;


        return 0;
}

这样做并不好。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签