“默认构造”内置类型? [英] "Default construction" of built-in types?

查看:53
本文介绍了“默认构造”内置类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在模板中遇到了一些代码,默认构造了一个类型为T的

对象传递给另一个函数...


SomeFunction( T());


实例化该模板的代码将T指定为int。


正确的程序行为依赖于默认构造" int为零。


引导我进入下面的例子......


int main()

{

int i; //未初始化!预计自动内置类型


int j = int(); //初始化为零!不确定会发生什么


返回0;

}


这样的默认构造是什么?内置类型标准C ++?


char()== 0

int()== 0


等...


谢谢

解决方案

" DaKoadMunky" <哒********* @ aol.com>写了...

我最近在一个模板中遇到了一些代码,默认构造了一个类型为T的
对象传递给另一个函数...

SomeFunction (T());

实例化该模板的代码将T指定为int。

正确的程序行为依赖于默认构造。 int是
零。


" Default-initialised"是正确的术语。并且,是的,它应该是

初始化为零。

这引导我进入以下示例...

int main( )
{
int i; //未初始化!预计自动内置式

int j = int(); //初始化为零!不确定会发生什么


评论的第二部分是什么意思?

返回0;
}
<这种默认结构是什么?内置类型标准C ++?




是的。


Victor

>> int j = int(); //初始化为零!不确定会发生什么


评论的第二部分是什么意思?




评论本来可以延长在其他编译器,平台等上说

,因为我不知道零的初始化是否是标准的。

Victor Bazarov写道:

" DaKoadMunky" <哒********* @ aol.com>写道...

我最近在一个模板中遇到了一些代码,默认构造了一个类型为T的
对象传递给另一个函数...

SomeFunction(T());

实例化该模板的代码将T指定为int。

正确的程序行为依赖于默认构造。 int is



零。

"默认初始化"是正确的术语。并且,是的,它应该被初始化为零。

这引导我进入以下示例...
int main()
{i /; //未初始化!预计自动内置式

int j = int(); //初始化为零!不确定会发生什么



评论的第二部分是什么意思?

return 0;
}

这样的默认构造是什么?内置类型标准C ++?



是的。

Victor




我不相信你,所以我查了一下,果然,这是正确的。


到OP:

标准说初始化程序为()的任何对象是

" value-initialized",对于POD类型,value-initialized"定义

为零初始化。有关详细信息,请参阅第8.5节。


Alan


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

解决方案

"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


>> 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:

"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


这篇关于“默认构造”内置类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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