By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

"Default construction" of built-in types?

P: n/a
I recently came across some code in a template that default constructed an
object of type T to pass to another function...

SomeFunction(T());

The code that instantiates that template specifies T as an int.

Proper program behavior relies on that "default constructed" int being zero.

That lead me to the following example...

int main()
{
int i; //Uninitialized! Expected for automatic of built-in type

int j = int(); //Initialized to zero! Not sure what to expect

return 0;
}

Is such "default construction" of built-in types standard C++?

char() == 0
int()==0

etc...

Thanks
Jul 22 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"DaKoadMunky" <da*********@aol.com> wrote...
I recently came across some code in a template that default constructed an
object of type T to pass to another function...

SomeFunction(T());

The code that instantiates that template specifies T as an int.

Proper program behavior relies on that "default constructed" int being zero.

"Default-initialised" is the proper term. And, yes, it is supposed to be
initialised to zero.

That lead me to the following example...

int main()
{
int i; //Uninitialized! Expected for automatic of built-in type

int j = int(); //Initialized to zero! Not sure what to expect
What do you mean by the second part of the comment?

return 0;
}

Is such "default construction" of built-in types standard C++?


Yes.

Victor
Jul 22 '05 #2

P: n/a
>> int j = int(); //Initialized to zero! Not sure what to expect

What do you mean by the second part of the comment?


Comment could have been extended to say "on other compilers, platforms, etc.."
given that I didn't know if the initialization to zero was standard.
Jul 22 '05 #3

P: n/a
Victor Bazarov wrote:
"DaKoadMunky" <da*********@aol.com> wrote...
I recently came across some code in a template that default constructed an
object of type T to pass to another function...

SomeFunction(T());

The code that instantiates that template specifies T as an int.

Proper program behavior relies on that "default constructed" int being


zero.

"Default-initialised" is the proper term. And, yes, it is supposed to be
initialised to zero.

That lead me to the following example...

int main()
{
int i; //Uninitialized! Expected for automatic of built-in type

int j = int(); //Initialized to zero! Not sure what to expect

What do you mean by the second part of the comment?

return 0;
}

Is such "default construction" of built-in types standard C++?

Yes.

Victor


I didn't believe you, so I looked it up, and sure enough, that is correct.

To the OP:
The standard says that any object whose initializer is () is
"value-initialized", and for POD types, "value-initialized" is defined
as being "zero-initialized". Refer to Section 8.5 for more info.

Alan
Jul 22 '05 #4

P: n/a

"Alan Johnson" <al****@mailandnews.com> wrote:
The standard says that any object whose initializer is () is
"value-initialized", and for POD types, "value-initialized" is defined
as being "zero-initialized". Refer to Section 8.5 for more info.


Ah-ha! I ran into trouble at work with a struct which
I had instantiated inside a function like so:

MyStruct Blat;

The struct had an int member which I expected to be
initialized to 0, but it actually had an initial value
of 512 !

So you're saying if I had instantiated it like this, instead:

MyStruct Blat ();

The numerical members would be initialized to 0 ?

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 22 '05 #5

P: n/a
* "Robbie Hatley" <lonewolfintj at pacbell dot net>:

"Alan Johnson" <al****@mailandnews.com> wrote:
The standard says that any object whose initializer is () is
"value-initialized", and for POD types, "value-initialized" is defined
as being "zero-initialized". Refer to Section 8.5 for more info.


Ah-ha! I ran into trouble at work with a struct which
I had instantiated inside a function like so:

MyStruct Blat;

The struct had an int member which I expected to be
initialized to 0, but it actually had an initial value
of 512 !

So you're saying if I had instantiated it like this, instead:

MyStruct Blat ();

The numerical members would be initialized to 0 ?


With a compiler that conforms in this respect, yes.

It is not a feature you can rely on, though.

Please stop changing the subject line, as it messes up the threading in
most newsreaders.

--
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?
Jul 22 '05 #6

P: n/a
> So you're saying if I had instantiated it like this, instead:

MyStruct Blat ();

The numerical members would be initialized to 0 ?

--
Cheers,
Robbie Hatley


That sez "Blat is a function that returns MyStruct".

You need thus:

MyStruct Blat = MyStruct ();

or

MyStruct Blat (MyStruct ());

This otherwise silly system exists so STL containers can default-construct
primitives without leaking garbage into them.

int x;

The act of using 'x' is now undefined for most operations except assignment.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #7

P: n/a
* Phlip:
So you're saying if I had instantiated it like this, instead:

MyStruct Blat ();

The numerical members would be initialized to 0 ?


That sez "Blat is a function that returns MyStruct".


Yes -- in my earlier replier I didn't recognize that... :-(

--
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?
Jul 22 '05 #8

P: n/a
"Phlip" <ph*******@yahoo.com> wrote:
So you're saying if I had instantiated it like this, instead:

MyStruct Blat ();

The numerical members would be initialized to 0 ?

--
Cheers,
Robbie Hatley
That sez "Blat is a function that returns MyStruct".


OOPS! Right, that's a prototype that says "I'll be
defining a function Blat later that takes no arguments and
returns a MyStruct". I really missed that one.
You need thus:

MyStruct Blat = MyStruct ();

or

MyStruct Blat (MyStruct ());


I don't like those.

The first one calls two constructors instead of just one:

MyStruct Blat = MyStruct (); // Default + Assignment

As for the second one:

MyStruct Blat (MyStruct ());

That compiles, but yields a function, not an object.
I think you you just declared a function which returns
a MyStruct, and takes as an argument a nameless pointer
to a nameless function which takes no arguments and
returns a MyStruct. Not a very useful concept. :-)

I think I like the approach I ended up using, after all:

MyStruct
{
MyStruct () : asdf(0), qwer(0) {}
int asdf;
int qwer;
};

int main(void)
{
MyStruct Blat; // Zeros members; calls only one constructor
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 22 '05 #9

P: n/a
On Sun, 27 Jun 2004 23:48:33 -0700, "Robbie Hatley" <lonewolfintj at
pacbell dot net> wrote:
You need thus:

MyStruct Blat = MyStruct ();

or

MyStruct Blat (MyStruct ());
I don't like those.

The first one calls two constructors instead of just one:

MyStruct Blat = MyStruct (); // Default + Assignment


You meant default and copy. But all compilers I know of elide the
temporary (as is allowed by the standard) and therefore don't make the
copy.

As for the second one:

MyStruct Blat (MyStruct ());

That compiles, but yields a function, not an object.
You need:

MyStruct Blat ((MyStruct()));

which is getting silly...
I think I like the approach I ended up using, after all:

MyStruct
{
MyStruct () : asdf(0), qwer(0) {}
int asdf;
int qwer;
};

int main(void)
{
MyStruct Blat; // Zeros members; calls only one constructor
}


Yup, that's the best plan, but in generic code where you want default
initialization but don't know the type, you should probably do:

T t = T();

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #10

P: n/a
tom_usenet wrote:
As for the second one:

MyStruct Blat (MyStruct ());

That compiles, but yields a function, not an object.


You need:

MyStruct Blat ((MyStruct()));

which is getting silly...


I suspect you both have that one wrong.

MyStruct Blat ((MyStruct())); can't be different from MyStruct Blat
(MyStruct()); or MyStruct Blat ((((((((MyStruct()))))))));. Many types can
take extra parens.

MyStruct() is an rvalue, so the compiler sees Blat's input argument, not a
type for a function.

BTW use T t = T(), just as a style guideline.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #11

P: n/a
Phlip wrote in news:uo***************@newssvr31.news.prodigy.com in
comp.lang.c++:
tom_usenet wrote:
>As for the second one:
>
> MyStruct Blat (MyStruct ());
>
>That compiles, but yields a function, not an object.
You need:

MyStruct Blat ((MyStruct()));

which is getting silly...


I suspect you both have that one wrong.

MyStruct Blat ((MyStruct())); can't be different from MyStruct Blat
(MyStruct()); or MyStruct Blat ((((((((MyStruct()))))))));. Many types
can take extra parens.


(int a); // not a legal declaration.
int (a); // legal.

MyStruct() is an rvalue, so the compiler sees Blat's input argument,
not a type for a function.

BTW use T t = T(), just as a style guideline.


Yup.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #12

P: n/a

"tom_usenet" <to********@hotmail.com> wrote in message
news:1l********************************@4ax.com...
On Sun, 27 Jun 2004 23:48:33 -0700, "Robbie Hatley" <lonewolfintj at
pacbell dot net> wrote:
You need thus:

MyStruct Blat = MyStruct ();

or

MyStruct Blat (MyStruct ());


I don't like those.

The first one calls two constructors instead of just one:

MyStruct Blat = MyStruct (); // Default + Assignment


You meant default and copy. But all compilers I know of elide the
temporary (as is allowed by the standard) and therefore don't make the
copy.


It was always my understanding that, even though this looks like an
assignment, it is not. It is still just a constructor call in another form.
Any talk of the compiler optimizing away the assignment is moot.

DrX
Jul 22 '05 #13

P: n/a
In message <cb*********@cui1.lmms.lmco.com>, Xenos
<do**********@spamhate.com> writes

"tom_usenet" <to********@hotmail.com> wrote in message
news:1l********************************@4ax.com.. .
On Sun, 27 Jun 2004 23:48:33 -0700, "Robbie Hatley" <lonewolfintj at
pacbell dot net> wrote:
>> You need thus:
>>
>> MyStruct Blat = MyStruct ();
>>
>> or
>>
>> MyStruct Blat (MyStruct ());
>
>I don't like those.
>
>The first one calls two constructors instead of just one:
>
> MyStruct Blat = MyStruct (); // Default + Assignment


You meant default and copy. But all compilers I know of elide the
temporary (as is allowed by the standard) and therefore don't make the
copy.


It was always my understanding that, even though this looks like an
assignment, it is not. It is still just a constructor call in another form.
Any talk of the compiler optimizing away the assignment is moot.

Maybe so, but talk of the compiler optimizing away the
_copy-construction_ (12.8.15) is perfectly valid ;-).

--
Richard Herring
Jul 22 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.