Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum type value as the length of a array in C++

As we all know, the array length in C++ must be determined. Then we can use:

const int MAX_Length=100;

or:

#define MAX_LENGTH 100

to determined the length of an array before compilation. But, when I read the book c++ primer by lippman, in chapter 3.5.1 in 5th Edition, it says: the length of an array must be an constant expression. Then the problem comes:

typedef enum Length{LEN1=100, LEN2, LEN3, LEN4}LEN; 
LEN MAX_Length=LEN2;  //101
int iArray[LEN2];     //attention

the code compiled successfully in mingw32-g++. But failed in VS2008, and the errors are:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'iArray' : unknown size

And I think the enum value is constant, so it should be used as an array's length. right?

I am confused, could you help me? Thank you.

like image 561
Kevin Wang Avatar asked Aug 20 '14 02:08

Kevin Wang


People also ask

What is the starting code value of enum in C++?

By default, the starting code value of the first element of enum is 0 (as in the case of array) . But it can be changed explicitly. And, The consecutive values of the enum will have the next set of code value (s).

What are the values of constants and enum variables?

By default, the values // of the constants are as follows: // constant1 = 0, constant2 = 1, constant3 = 2 and // so on. enum flag {constant1, constant2, constant3, ....... }; Variables of type enum can also be defined. They can be defined in two ways:

How to get the length of an array in C++?

The simplest procedural way to get the value of the length of an array is by using the sizeof operator. First you need to determine the size of the array. Then you need to divide it by the size of one element. It works because every item in the array has the same type, and as such the same size. Example:

What is enumeration in C++?

Last Updated : 21 Dec, 2018. Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. enum State {Working = 1, Failed = 0}; The keyword ‘enum’ is used to declare new enumeration types in C and C++.


Video Answer


1 Answers

In both C++11 and C++03 enumerators(unscoped enums in C++11) are integer constant expressions and therefore usable an array bounds. We can see this for C++11 by going to the draft C++11 standard section 5.19 [expr.const] which says:

An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression. [ Note: Such expressions may be used as array bounds (8.3.4, 5.3.4), as bit-field lengths (9.6), as enumerator initializers if the underlying type is not fixed (7.2), as null pointer constants (4.10), and as alignments (7.6.2). —end note ]

and or C++03 we can see this from the draft C++03 standard or the closest we can get same section paragraph 1 which says:

[...]An integral constant-expression can involve only literals of arithmetic types (2.13, 3.9.1), enumerators, non-volatile const variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type template parameters of integral or enumeration types, and sizeof expressions[...]

On rextester this code compiles fine for VC++, so this is no longer an issue in current versions this must have been a bug in 2008 that was eventually fixed. Also tested on webcompiler which was last updated December 3rd 2015 so this works on one of the latest releases as well.

One alternative may be to use a const int for example:

const int len = LEN2 ;

this will depends on whether Visual Studio 2008 considers enumerators to not be integer constant expressions or whether it is just in the context of array bounds, hopefully it is just the later.

C++98

As far as I can tell this also applies to C++98 as well, both gcc and clang allow this when using -std=c++98, there are no draft C++98 standards available to the public so I can confirm beyond that.

like image 108
Shafik Yaghmour Avatar answered Sep 19 '22 00:09

Shafik Yaghmour