Value initialization. . .

J

JKop

The following code demonstrates that when you do:

AnyNonPOD &blah = *new AnyNonPOD();


that the object's member variables *don't* get zero initialized.


I've been trying to find a way to create a non-const object of a non-POD
class and have all its member variables initialized to zero... the only
catch is that this non-POD will be non-copyable(ie. private copy
constructor). Anyway, the following code prints the following on my system:

I'm non-const!
Is it zero initialized: false


Here's the code:

#include <iostream>

class NonCopyable
{
private:

NonCopyable(NonCopyable const &);

public:

NonCopyable() {}
//Note that there's no initializer list to set
//things to zero.

unsigned int a;
unsigned char b;
void* p_c[9999999];
};


void Func(NonCopyable &)
{
std::cout << "I'm non-const!\n";
}

void Func(NonCopyable const &)
{
std::cout << "I'm const!\n";
}

int main()
{
NonCopyable &blah = *new NonCopyable();


Func(blah); //Making sure it's not a function declaration!


bool is_zero_initialized = true;

if ( !blah.a && !blah.b )
{
for(unsigned i = 0; i < 9999999; ++i)
{
if ( blah.p_c )
{
is_zero_initialized = false;
break;
}
}
}
else is_zero_initialized = false;

std::cout << "Is it zero initialized: " << ( is_zero_initialized ?
"true" : "false" );

delete &blah;
}



Overall, I'm trying to get around one of the top "bullshit complications" in
C++, in that you can't do:

Class object();

instead having to do:

Class object = Class();


which creates 2 problems.

Problem A) A temporary
Problem B) Copy constructor must be public


If only there were some clarity, as in:

Function Declaration: extern Class object();
Object Definition: class Class object();

and that when you did:

Class object();
or
Class object(void);

that it's a function declaration (which is how it is now).



-JKop
 
G

Gernot Frisch

#include said:
class NonCopyable
{
private:

NonCopyable(NonCopyable const &);

public:

NonCopyable() {}
//Note that there's no initializer list to set
//things to zero.

unsigned int a;
unsigned char b;
void* p_c[9999999];
};


void Func(NonCopyable &)
{
std::cout << "I'm non-const!\n";
}

void Func(NonCopyable const &)
{
std::cout << "I'm const!\n";
}

int main()
{
NonCopyable &blah = *new NonCopyable();

const NonCopyable &blah = *new NonCopyable();
 
J

JKop

const NonCopyable &blah = *new NonCopyable();


.. . . Why?


Perhaps you're mistaken with:


NonCopyable const &blah = NonCopyable();


the "const" modifier would be necessary there alright. (I think)

....but anyway you'd have an invalid reference. (I think)


-JKop
 
V

Victor Bazarov

Add
NonCopyable& operator=(NonCopyable const&);
public:

NonCopyable() {}
//Note that there's no initializer list to set
//things to zero.

unsigned int a;
unsigned char b;
void* p_c[9999999];
};


void Func(NonCopyable &)
{
std::cout << "I'm non-const!\n";
}

void Func(NonCopyable const &)
{
std::cout << "I'm const!\n";
}

int main()
{
NonCopyable &blah = *new NonCopyable();


const NonCopyable &blah = *new NonCopyable();

Func(blah); //Making sure it's not a function declaration!


bool is_zero_initialized = true;

if ( !blah.a && !blah.b )
{
for(unsigned i = 0; i < 9999999; ++i)
{
if ( blah.p_c )
{
is_zero_initialized = false;
break;
}
}
}
else is_zero_initialized = false;

std::cout << "Is it zero initialized: " << ( is_zero_initialized
?
"true" : "false" );

delete &blah;
}

Victor
 
T

Tom Widmer

The following code demonstrates that when you do:

AnyNonPOD &blah = *new AnyNonPOD();


that the object's member variables *don't* get zero initialized.


I've been trying to find a way to create a non-const object of a non-POD
class and have all its member variables initialized to zero... the only
catch is that this non-POD will be non-copyable(ie. private copy
constructor). Anyway, the following code prints the following on my system:

I'm non-const!
Is it zero initialized: false


Here's the code:

#include <iostream>

class NonCopyable
{
private:

NonCopyable(NonCopyable const &);

public:

NonCopyable() {}
//Note that there's no initializer list to set
//things to zero.

If you want value-initialization to zero members, you mustn't write a
default constructor. I think zeroing only occurs if there is no user
declared default constructor. But if you don't write a default
constructor, but do declare a copy constructor, your object won't be
default constructible! Hence your post decribes a non-issue (since you
can't zero initialize non-copyable objects for reasons other than you
describe).
Overall, I'm trying to get around one of the top "bullshit complications" in
C++, in that you can't do:

Class object();

instead having to do:

Class object = Class();


which creates 2 problems.

Problem A) A temporary

Which all compilers elide.
Problem B) Copy constructor must be public

If the copy constructor is private, then in order for the object to be
default constructible, it must have a user-declared default
constructor, which means you just write:

Class object;
If only there were some clarity, as in:

Function Declaration: extern Class object();
Object Definition: class Class object();

The above is a function declaration too. Perhaps something like:

Class object(default); //performs value-initialization.

This would be useful for generic code. But currently not all compilers
implement the TC1 value-initialization changes to the standard anyway.

Tom
 
R

Ron Natalie

JKop said:
I've been trying to find a way to create a non-const object of a non-POD
class and have all its member variables initialized to zero...

The issue is NO longer POD vs. non-POD in standard C++. The issue is
whether there is a user defined default constructor.

Overall, I'm trying to get around one of the top "bullshit complications" in
C++, in that you can't do:

Class object();

instead having to do:

Class object = Class();

What bullshit compllication are you talking about.
Class object();
is a function decaration.
Class object;
is a default constructed object (for non POD's).
 
O

Old Wolf

Gernot Frisch said:
const NonCopyable &blah = *new NonCopyable();
This is a terrible idea, as you have to remember to do
"delete &blah" later. This will confuse almost anybody
else looking at the code, because it is usual to refer
to new'd objects by a pointer. Especially if the 'new' and
'delete' are not nearby in the code.

If you really feel the need to use . notation instead of
->, go:
std::auto_ptr<NonCopyable> ptr(new NonCopyable());
NonCopyable const &blah = *ptr;
....
// no delete required
 
J

JKop

This is a terrible idea, as you have to remember to do
"delete &blah" later.


I have to remember to consume water every three days. . . ain't dead yet.

This will confuse almost anybody
else looking at the code, because it is usual to refer
to new'd objects by a pointer. Especially if the 'new' and
'delete' are not nearby in the code.


It's perfectly legal C++, it one is confused by it, then they don't know
C++.

If you really feel the need to use . notation instead of
->, go:
std::auto_ptr<NonCopyable> ptr(new NonCopyable());
NonCopyable const &blah = *ptr;
...
// no delete required

Or... I could use *my* method, which is approx. 5 billion times more
efficient.


-JKop
 
R

Rolf Magnus

JKop said:
I have to remember to consume water every three days. . . ain't dead yet.




It's perfectly legal C++,

It is. However, it's very ugly, too. The only thing that could prevent you
from writing ugly code is experience in reading ugly code.
it one is confused by it, then they don't know C++.

People will understand it, but will need longer, because there is no
sensible reason to do what you are doing. And it will always take people
longer to understand unreasonable things.
Or... I could use *my* method, which is approx. 5 billion times more
efficient.

I doubt you ever tested what the difference is.
 
R

Rolf Magnus

Ron said:
The issue is NO longer POD vs. non-POD in standard C++. The issue is
whether there is a user defined default constructor.



What bullshit compllication are you talking about.
Class object();
is a function decaration.
Class object;
is a default constructed object (for non POD's).

And

Class object = {0};

is a zero initialized POD object, which seems to be what the OP is searching
for.
 
G

Gernot Frisch

It is. However, it's very ugly, too. The only thing that could
prevent you
from writing ugly code is experience in reading ugly code.

Agree. I juist wanted to solve the problem, not the style. But, I'd
never write that code. I don't even like the overloading of -> if it
can be avoided.
I doubt you ever tested what the difference is.

I think auto_ptr is quite fast. There's not much difference here. But
reading this code is as difficult as the original method (for me). I
don't´like auto_ptr's. new. delete. It's that simple. I can't think of
anything why I would like to have garbage collectors (that's why C# is
suppoest to be so much "easier").
I'm goint OT now, sorry.
-Gernot
 
T

Tom Widmer

If the Standard enforced that, I wouldn't have any problems whatsoever.

Alas, the Standard does not.

For classes with private copy constructors, why don't you just write:

MyClass myObject; //default constructor called.

Tom
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top