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 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
>> 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.
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
"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 =---
* "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?
> 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
* 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?
"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 =---
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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..
|
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.
|
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?
|
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
| |
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).
|
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 !
|
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.
|
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;
};
|
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:\.
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |