473,749 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Default construction" of built-in types?

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
13 2279
"DaKoadMunk y" <da*********@ao l.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
>> 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
Victor Bazarov wrote:
"DaKoadMunk y" <da*********@ao l.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

"Alan Johnson" <al****@mailand news.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
* "Robbie Hatley" <lonewolfintj at pacbell dot net>:

"Alan Johnson" <al****@mailand news.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
> 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
* 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
"Phlip" <ph*******@yaho o.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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5574
by: C# Learner | last post by:
Is it possible to set a command button to the "default" button of the form? Here's an example of what I mean -- press Start -> Run. Notice that the "OK" button has a dark rectangle around it, and when you press ENTER, this "OK" button's Click event handler is eventually fired. I was expecting Button to have a Default property, but can't seem to find anything like it...
1
1925
by: Brian | last post by:
Is there a way to change the default "using" statements that are created by Visual Studio when you create a new class? Do I have to create a new file type? So, instead of choosing "New -> Class" I would have to choose "New -> MyClass". Basically, I want to add "Using System.IO;" and "Using System.Diagnostics" so that when I create a new class, they will automatically show up. VS Macro?? Help..
2
1505
by: ABC | last post by:
How to return the full virtual path if I pass the url as "~/default.aspx"? I expected this should result as http://aaa.bbb.com/default.aspx.
1
1780
by: Jeff Brown | last post by:
Hi, I'm trying to implement the functionality of the generic expression, "default(T)", but for a given instance of the Type class. The ideal would be if the Type class had a method like "object Type.MakeDefaultInstance()" -- but it doesn't. Here's my attempt, but it seems rather kludgy. Is there a better way?
0
1133
by: John Paul. A | last post by:
Hi, I want to get the port number of "Default Web Site" from IIS. Can you please tell me how to get it using the namespace System.DirectoryServices. Thanks, John
3
2903
by: per9000 | last post by:
Hi, can I print some default text on the console when doing a ReadLine? A silly example below shows two small scenarios. In the first a user is told what he appears to be called, and asks for a name. In the second case I want the the user to be able to just press return to accept the username of the system. (I understand it does not work the way I do it below - I just want to illustrate what I want).
1
12187
by: Rob | last post by:
I am doing a simple setup deployment.... everything is fine but the program installs to the Program Files\Default Company Name.... I hear that is derived from the "Manufacturer".... Where do you change this ? How do you get there ? Thanks !
2
4692
by: TheSteph | last post by:
Hi, does anybody know how I can get the "default system Border Width " ? example : if I draw a TextBox with the default 3D border, what is the with (thikness) of the border ? Thanks for your help ! Steph3.
4
1426
by: Ed | last post by:
Hi, guys, Here is a simple template class definition: template <typename T = int> class Point { public: T X; T Y; T Z; };
1
9622
by: Simon van Beek | last post by:
Dear reader, Under | Tools | Options | the form Options will open. Under the tab you find the field "Default database folder". By opening the form this field shows on my pc I:\. I can't close the form because then the message pups up "MS Office Access can't change the work directory to I:\ " If I change it to D:\ I can close the form but by opening the form again the field is still showing I:\.
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.