Initialization of numeric (intrinsic type) data members

L

Lionel B

This always confuses me. In the example below I would like x to be zero-
initialised on default construction of a struct A object (there is no
other initialisation/construction code required for default
construction). Which form is most appropriate?

struct A
{
double x;

// first form
// No user-supplied default constructor

// second form
A() {}

// third form
A() : x() {}

// fourth form
A() : x(0.0) {}
};

void foo()
{
A a;
/* do stuff, assuming a.x == 0.0 */
}

Could some kind soul also point me to the relevant entry in the standard?

Thanks,
 
N

Neelesh Bodas

This always confuses me. In the example below I would like x to be zero-
initialised on default construction of a struct A object (there is no
other initialisation/construction code required for default
construction). Which form is most appropriate?

struct A
{
double x;

// first form
// No user-supplied default constructor

// second form
A() {}

// third form
A() : x() {}

// fourth form
A() : x(0.0) {}

third and forth both will work.
};

void foo()
{
A a;
/* do stuff, assuming a.x == 0.0 */

}

Could some kind soul also point me to the relevant entry in the standard?

12.1 (7) The implicitly-defined default constructor performs the set
of initializations of the class that would be performed by a user-
written default constructor for that class with an empty mem-
initializer-list and an empty function body

(which means that your first form and second form are identical)

12.6.2(4) If a given nonstatic data member or base class is not named
by a meminitializerid
(including the case where there is no meminitializerlist because the
constructor has no ctorinitializer), then

- If the entity is a non-static data member of (possibly cv-qualified)
class type (or array thereof) or a base class, and the entity class is
a non-POD class, the entity is default-initialized (8.5). If the
entity is a non-static data member of a const-qualified type, the
entity class shall have a user-declared default constructor.

- Otherwise, the entity is not initialized. If the entity is of const-
qualified type or reference type, or of a (possibly cv-qualified) POD
class type (or array thereof) containing (directly or indirectly) a
member of a const-qualified type, the program is ill-formed.

According to "otherwise" clause above, x will be left uninitialized by
the default constructor.

-N
 
L

Lionel B

third and forth both will work.

Right. I actually use the third form in the "real" code (the numeric type
is in fact a template argument).

[...]

Thanks very much,
 
J

James Kanze

This always confuses me. In the example below I would like x to be zero-
initialised on default construction of a struct A object (there is no
other initialisation/construction code required for default
construction). Which form is most appropriate?
struct A
{
double x;
// first form
// No user-supplied default constructor

Does nothing...
// second form
A() {}

Does nothing...
// third form
A() : x() {}

Either "value initializes" or "default initializes" x, according
to which version of the standard you refer to. In the case of a
POD type like double, there's no difference, however; both mean
that x will be "zero initialized" (which is the equivalent of
"x(0)"; i.e. it initializes the value with the results of
converting the integral constant 0 to the appropriate type).

This features was added in the 1998 standard; it wasn't present
in pre-standard C++, and some very old compilers might not
implement it. (In pre-standard C++, it did nothing, like the
two previous versions.)
// fourth form
A() : x(0.0) {}

Initializes x with 0.0. If that's what you want, then this is
the most appropriate form. (The "x()" form might be more
appropriate, of course, for more complicated types, or in a
template.)
void foo()
{
A a;
/* do stuff, assuming a.x == 0.0 */
}
Could some kind soul also point me to the relevant entry in
the standard?

Which version of the standard:)? The different types of
initialization are described in detail in §8.5, from about
paragraph 5 on. The details do differ depending on the version
of the standard, however. (I think the difference is really
only relevant for class types with non-trivial compiler
generated constructors, but I wouldn't swear to it.)
 
V

Victor Bazarov

James said:
Which version of the standard:)? The different types of
initialization are described in detail in §8.5, from about
paragraph 5 on. The details do differ depending on the version
of the standard, however.

For some freaky reason, in *my* twisted understanding of the
standardisation process, there is always one and only Standard.
Why muddy the waters by the terms like "version of the standard"
when any earlier "version" is *always* superceded by the later
"version"? Yes, one can always find the Gloekenspiel C++ (or
however it was called) and try using it, but it's not the matter
of "version of the standard" but rather of compliance to _the_
Standard (which BTW has already been in force for ~4 years now).

V
 
J

James Kanze

For some freaky reason, in *my* twisted understanding of the
standardisation process, there is always one and only Standard.

In theory, you're right. But the one today isn't the same as
the one yesterday, and compilers vary with regards to which one,
if any, they support.
Why muddy the waters by the terms like "version of the standard"
when any earlier "version" is *always* superceded by the later
"version"?

Because in practice, it isn't.
Yes, one can always find the Gloekenspiel C++ (or
however it was called) and try using it, but it's not the matter
of "version of the standard" but rather of compliance to _the_
Standard (which BTW has already been in force for ~4 years now).

The official version is C++ 2003. Regretfully, I don't have
that version handy. And g++ only accepts -std=c++98---it
doesn't support the changes introduced in 2003. And I would
imagine that most people would prefer code that won't suddenly
be broken when the next version of the standard appears
(promessed before 2009, but I really don't think that's
possible), so what the current draft says isn't without interest
either.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top