默认初始化与价值初始化 [英] Default Initialization Vs. Value Initialization

查看:92
本文介绍了默认初始化与价值初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们之间有什么区别?


请采取以下措施:


#include< iostream>

struct Blah

{

int k;

const char * p_b;

std :: string c;

};

我曾经认为你做过:

Blah poo = Blah();

其所有成员都获得了默认初始化,结果是:


poo.k == 0

poo.p_b == 0 (空指针值)

poo.c(获取默认的std :: string构造函数,不带参数调用)

我在上面是正确的,是吗?但现在我听说事情已经发生了变化,并且




Blah poo = Blah();


结果在价值初始化中...


那么价值初始化是什么?它与默认

初始化有什么不同?

-JKop

What''s the difference between them?

Take the following:

#include <iostream>
struct Blah
{
int k;
const char* p_b;
std::string c;
};
I used to think that when you did:
Blah poo = Blah();
that all its members got "default initialized", which resulted in:

poo.k == 0
poo.p_b == 0 (null pointer value)
poo.c (Gets the default std::string constructor called with no arguments)
I''m correct in the above, yes? But now I hear that things have changed and
that

Blah poo = Blah();

results in "value intialization"...

So what''s "value intialization" and how is it different from "default
initialization"?
-JKop

推荐答案



" JKop" < NU ** @ NULL.NULL>在消息新闻中写道:oJ ******************* @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message news:oJ*******************@news.indigo.ie...
那么'是什么'的价值intialization"它与默认
初始化有什么不同?
So what''s "value intialization" and how is it different from "default
initialization"?




要理解值初始化,你必须明白POD类型有

默认初始化(零初始化),有时默认初始化只是省略。

[我认为这是语言的主要缺陷,但它使性能兼容

与C因此很难说服人们愚蠢。]


问题是类型如:

struct S {

int i;

T t;

};


获取不同的行为基于T类型的旧标准(如果T是POD则S是

POD并且你得到零初始化,因此我初始化为零,如果T不是POD,那么我不会

初始化,因为S的默认构造函数没有触及它。


为了解决这个问题,大多数发生默认初始化的地方都被替换了值初始化。

和值初始化而不是使用PODness一个决定因素的查找用户定义的构造函数的存在,以确定是否递归值初始化或仅运行

consturctor ......



To understand value initialization, you have to understand that while POD types have
default initialization (zero initialization), some times the default initialization is just omitted.
[This I see as a major defect in the language, but it makes it performance compatible
with C so it''s hard to convince people of the stupidity.]

The problem is that types such as:
struct S {
int i;
T t;
};

Get different behavior in the old standard based on the type of T (if T is POD then S is
POD and you get zero initialization and i is hence zero initialized, if T is not POD, then i doesn''t
get initialized because S''s default constructor doesn''t touch it).

To fix this, most places where default initialization occured are replaced with value initialization.
And value initialization rather than using "PODness" of a determining factor looks for the presence
of a user defined constructor to determine whether to recursively value-initialize or to just run the
consturctor...


Ron Natalie发布:
Ron Natalie posted:

" JKop" < NU ** @ NULL.NULL>在消息中写道
新闻:oJ ******************* @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message
news:oJ*******************@news.indigo.ie...
那么''' s价值初始化和默认初始化有什么不同


要理解值初始化,你必须
So what''s "value intialization" and how is it different from "default initialization"?

To understand value initialization, you have to



了解POD类型有默认初始化(零
初始化),有时默认初始化只是省略。 [这个我认为
是该语言的一个主要缺陷,但是它使得它与C兼容,因此很难说服人们愚蠢。]

问题是类型如:
struct S {
int i;
T t;
};

在旧标准中获得不同的行为基于
类型的T(如果T是POD,那么S是POD,你得到零初始化,
i因此初始化为零,如果T不是POD,那么我不会得到
初始化,因为S的默认构造函数没有触及它。

为了解决这个问题,大多数默认初始化
发生的地方都被值初始化所取代。并且值
初始化而不是使用PODness。一个决定因素寻找
是否存在用户定义的构造函数来确定是否以
递归值初始化或仅运行consturctor ...


understand that while POD types have default initialization (zero initialization), some times the default initialization is just omitted. [This I see as a major defect in the language, but it makes it performance compatible with C so it''s hard to convince people of the stupidity.]

The problem is that types such as:
struct S {
int i;
T t;
};

Get different behavior in the old standard based on the type of T (if T is POD then S is POD and you get zero initialization and i is hence zero initialized, if T is not POD, then i doesn''t get initialized because S''s default constructor doesn''t touch it).

To fix this, most places where default initialization occured are replaced with value initialization. And value initialization rather than using "PODness" of a determining factor looks for the presence of a user defined constructor to determine whether to recursively value-initialize or to just run the consturctor...



好的......我想我明白了。鉴于:


struct Blah

{

int i;

std :: string k;

};

关于* old *标准,当你这样做时:


Blah poo = Blah();


然后poo.i没有...让我们只是称之为

初始化。

但是......当你这样做时,新标准: br />

Blah poo = Blah();


然后是poo.i *确实*初始化。

BTW,Blah是POD,对吗?我会把它分类为

无论如何...

我认为以下是

因素我是否正确渲染结构*不再是*一个POD:


A)构造函数(包括复制构造函数)

B)私有或受保护的成员


嗯...如果你看看Blah,它没有,所以你可以

定义一个如此:


std :: string a(" MONKEY!");


Blah poo = {5,a};


就这样它是POD ......对吗?

-JKop


Okay... I think I understand. Given:

struct Blah
{
int i;
std::string k;
};
With regard to the *old* standard, when you did:

Blah poo = Blah();

Then "poo.i" didn''t get... let''s just call it
"initialized".
But... with the new standard, when you do:

Blah poo = Blah();

then "poo.i" *does* get initialized.
BTW, Blah is a POD, right? I''d classify it as so in
anyway...
Would I be right in thinking that the following are the
factors which render a structure *no longer* a POD:

A) a constructor (including a copy constructor)
B) private or protected members

Well... if you look at Blah, it has neither, hence you can
define one as so:

std::string a("MONKEY!");

Blah poo = { 5, a };

And as such it''s a POD... right?
-JKop




" JKop" < NU ** @ NULL.NULL>在消息中写道

新闻:vV ******************* @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message
news:vV*******************@news.indigo.ie...
我是否正确认为以下是使结构*不再是* POD的因素:

A)构造函数(包括复制构造函数)


不相关。即使你没有提供构造函数,编译器也会给b
提供一个。

B)私人或受保护的成员
Would I be right in thinking that the following are the
factors which render a structure *no longer* a POD:

A) a constructor (including a copy constructor)
Not relevant. Even if you don''t supply a constructor, the compiler will
supply one.
B) private or protected members



也不相关。


POD类型是内置类型,或仅包含内置类型,或

包含只有POD成员(不是这样的指针)或这样的数组。 (我想

我已经覆盖了它,但也许没有。它已被描述很多次......在group.google.com上搜索
。)


-Howard



Also not relevant.

A POD type is either a built-in type, or contains only built-in types, or
contains only POD members (not pointer to such) or arrays of such. (I think
I''ve covered it, but maybe not. It''s been described many times..try
searching on groups.google.com.)

-Howard


这篇关于默认初始化与价值初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆