what's the difference between value-initialization and default-initialization?

A

Alf P. Steinbach

* Ron Natalie:
There is an implicitly defined consturctor (admittedly rarely actually
causing executable code), but it depends on what the definition of B
is.

Right. Sorry about the typos ("B" instead of "b", and unstated
assumption of PODness). The point remains: if B is a POD it will not be
initialized (the standard states in §8/9 that it will have an
"indeterminate initial value", as opposed to being default-initialized).

Not relevant to that point, but I think relevant to those who might read
this: regardless of B's PODness, without a user-defined constructor POD
members won't be initialized here (§12.1/7).

To ensure initialization you need a user-defined constructor, even
though as I exemplified earlier that's not an absolute guarantee, only
an effective guarantee for sensible code.

This is actually the who reason the term "value initialized" was
invented.

Nope. The object b above is not value-initialized. It's either not
initialized at all, or if non-POD, the default constructor is called.

Value initialization applies to an expression such as

B();

see §8.5/7, and the intent is that the effect of this expression should
not depend on whether there is a non-POD member or not: that this
expression should always give some initialization of all members,
zeroing all POD stuff.
 
J

James Kanze

Must have been a long day for Ron and Alf. The parameter to 'b'
has type 'pointer to function taking no arguments and returning B'.
It is the same as:
B b( B (*)() );

Is it ok to ignore the "*" while still have the same meaning?
"type arg" is not a "standard format". Some simple declarations
look like that, but the syntax for declarations is more complicated,
with "arg" sometimes coming in between different parts of "type".
For example, int x[5]

Indeed, :)
Huh? (Post some code to illustrate what you mean).

I made the mistake of thinking this init "b" and realised I was
wrong....
You have one step too many (I think). A temporary object is default-
constructed, and then 'b' is copy-constructed from that object.

I see, so the additional temp object is only created if a function is
returning, is it right?

Thanks
Jess
 
J

James Kanze

Is it ok to ignore the "*" while still have the same meaning?

More the reverse. According to the standard, it's permitted, je
even required, to pretend that the "(*)" is there, even when it
isn't. The type expression "B()" declares a function; if we
wanted to give it a name, we would write "B f()". The standard
says, however, that when we declare a function parameter to have
a type "function", it is treated as a "pointer to function",
much as when we declare an array, it is treated as "pointer".
 
J

Jess

More the reverse. According to the standard, it's permitted, je
even required, to pretend that the "(*)" is there, even when it
isn't. The type expression "B()" declares a function; if we
wanted to give it a name, we would write "B f()". The standard
says, however, that when we declare a function parameter to have
a type "function", it is treated as a "pointer to function",
much as when we declare an array, it is treated as "pointer".

I see, thanks a lot!

Jess
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top