enum value from array

F

Fraser Ross

char array[4]= "123";
enum { a=array[2], };

I'm not sure why the program doesn't compile. Is it to do with
initalisation of statics? I want to initialise enumerators with values
created from arrays.

Fraser.
 
N

Neelesh

char array[4]= "123";
enum { a=array[2], };

I'm not sure why the program doesn't compile.  Is it to do with
initalisation of statics?  

An enum initializer must be a integral constant expression, and
according to 5.19p1 of the C++ standard, An integral constant
expression can involve only literals , enumerators, const variables
etc. In particular, array references are not allowed.
I want to initialise enumerators with values created from arrays.

As far as C++ standard is concerned, that cannot be done. However,
observe that If you don't know the values at compile time, you can
anyway not initialize the enumeration members using those values. If
you know them, you don't need an array to initialize the enumeration
members.
 
N

Neelesh

char array[4]= "123";
enum { a=array[2], };
I'm not sure why the program doesn't compile.  Is it to do with
initalisation of statics?  

An enum initializer must be a integral constant expression, and
according to 5.19p1 of the C++ standard, An integral constant
expression can involve only literals , enumerators, const variables

BTW- not just any constvariables, but const variables of integral type
initialized with a constant expression.
 
F

Fraser Ross

Can a function return a compile time value created from array if it uses
constexpr? I don't actually need an enumerator. I want to test a value
derived from an array at compile time with static_assert, the value and
a value to be calculated at compile time.

Fraser.
 
N

Neelesh

Can a function return a compile time value created from array if it uses
constexpr?  I don't actually need an enumerator.  I want to test a value
derived from an array at compile time with static_assert, the value and
a value to be calculated at compile time.

Fraser.

Are you talking about BOOST_STATIC_ASSERT? Note that function cannot
"return" at compile time - function executes at run time. If you have
all values at compile time, you can easily use BOOST_STATIC_ASSERT.

It would be helpful to provide a minimal piece of code that
demonstrated the problem.
 
F

Fraser Ross

Are you talking about BOOST_STATIC_ASSERT? Note that function cannot
"return" at compile time - function executes at run time. If you have
all values at compile time, you can easily use BOOST_STATIC_ASSERT.


Anything of the kind would work the same. C++ Builder 2009 supports the
static_assert keyword.

I don't think I will get my assertion without constexpr.

Fraser.
 
A

Alf P. Steinbach

* Fraser Ross:
Anything of the kind would work the same. C++ Builder 2009 supports the
static_assert keyword.

I don't think I will get my assertion without constexpr.

It would be helpful to provide a minimal piece of code that
demonstrated the problem.
 
F

Fraser Ross

I want to obtain a value created from values in a static array and
compare it to the correct value with static_assert.

While looking into constexpr I found this:
struct V {
static const int g;
};
const int a = 10 * V::g; // dynamic initialization
const int V::g = 98; //initialization comes too late

Is the definition of a before g? Initialisation should be done in the
order of definitions. C++ Builder seems to use the order of
declarations.

Fraser.
 
N

Neelesh

I want to obtain a value created from values in a static array and
compare it to the correct value with static_assert.

While looking into constexpr I found this:
struct V {
 static const int g;};

const int a = 10 * V::g; // dynamic initialization
const int V::g = 98; //initialization comes too late

Is the definition of a before g?  Initialisation should be done in the
order of definitions.  C++ Builder seems to use the order of
declarations.

c++ standard requires (3.6.2 p1) that objects of POD type with static
storage duration that are initialized with constant expression are
initialized before any dynamic initalization. This means that in the
current case V::g will be initialized before a.
 
F

Fraser Ross

c++ standard requires (3.6.2 p1) that objects of POD type with static
storage duration that are initialized with constant expression are
initialized before any dynamic initalization. This means that in the
current case V::g will be initialized before a.

Thats only a part of a full explanation. To answer my own question the
first g is not a definition. Microsofts compiler also initialises in
the order of declartions and thats a compiler bug.

Fraser.
 
F

Fraser Ross

I confused myself. Microsofts compiler is ok but C++ Builder does not
compile this:
struct V {
static const int g;
};
const int V::g = 98; //initialization comes too late
const int a = 10 * V::g; // dynamic initialization
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
int b[a];
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top