Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > what's the difference between value-initialization and default-initialization?

Reply
Thread Tools

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

 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-02-2007
* Ron Natalie:
> Alf P. Steinbach wrote:
>
>>
>> Sorry that's incorrect. Specifically, without a user-defined
>> constructor,
>>
>> B b;
>>
>> locally in a function, does not initialize B (another example is use
>> in a union).
>>

> 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      05-02-2007
On May 2, 8:19 am, Jess <(E-Mail Removed)> wrote:
> On May 2, 10:30 am, Old Wolf <(E-Mail Removed)> wrote:
>
> > 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,
>
> > > Moreover, if this is a definition of "b", then why can I receive a B
> > > object after this execution, where's the invocation of the function "b"?

>
> > Huh? (Post some code to illustrate what you mean).

>
> I made the mistake of thinking this init "b" and realised I was
> wrong....
>
> > > I think the default constructor is called in the first case. For the
> > > second case, a default constructor is called for "B()", then a copy-
> > > constructor is called (by the compiler) to copy the "B" to a temporary
> > > result, and then another copy-constructor is called to copy the
> > > temporary B object to "b" to initialize "b", is this right?

>
> > 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



 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      05-02-2007
On May 2, 8:19 am, Jess <(E-Mail Removed)> wrote:
> On May 2, 10:30 am, Old Wolf <(E-Mail Removed)> wrote:


> > 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?


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".

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orient¨¦e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S¨¦mard, 78210 St.-Cyr-l'¨¦cole, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Jess
Guest
Posts: n/a
 
      05-04-2007
On May 3, 2:24 am, James Kanze <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 04-15-2011 04:00 AM
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
difference between between these "char"s arnuld C++ 33 03-05-2007 03:11 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments