initializer needs constant

M

Mantorok Redgormor

struct mystruct {
int a;
int b;
};

struct mystruct x = { 10, 10 }, y = x;

What section in the standard says that y needs a constant?
And why can't it resolve x so that y can have a copy of its values?
 
M

Mark A. Odell

(e-mail address removed) (Mantorok Redgormor) wrote in

struct mystruct {
int a;
int b;
};

struct mystruct x = { 10, 10 }, y = x;

What section in the standard says that y needs a constant?
And why can't it resolve x so that y can have a copy of its values?

const int foo = 10;
int bar = foo; <-- error: initializer element is not constant

const int foo = 10;

does not mean foo is a constant, it means the compiler will not allow you
to assign a value to foo other than an initializer. You need a real
constant, not a const variable.
 
A

A. Bolmarcich

struct mystruct {
int a;
int b;
};

struct mystruct x = { 10, 10 }, y = x;

What section in the standard says that y needs a constant?
And why can't it resolve x so that y can have a copy of its values?

In ISO/IEC 9899:1990 the "Constraints" subsection of the
"6.5.7 Initialization" section contains the sentence:

All the expressions in an initializer for an object that has
static storage duration or in an initializer list for an object
that has aggregate or union type shall be constant
expression.
 

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
474,102
Messages
2,570,645
Members
47,247
Latest member
GabrieleL2

Latest Threads

Top