C ++做一个POD typedef的值初始化? [英] Does C++ do value initialization of a POD typedef?

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

问题描述

C ++在简单的 POD typedefs上执行值初始化吗?

Does C++ do value initialization on simple POD typedefs?

假设

typedef T* Ptr;

Ptr()

做值初始化并保证等于 (T *)0

do value-initialization and guarantee to equal (T*)0?

例如

Ptr p = Ptr();
return Ptr();


推荐答案

对于 T T()值初始化类型 T 并生成一个右值表达式。

It does. For a type T, T() value-initializes an "object" of type T and yields an rvalue expression.

int a = int();
assert(a == 0);

与pod-classes相同:

Same for pod-classes:

struct A { int a; };
assert(A().a == 0);

对于没有用户声明的构造函数的一些非POD类也是如此:

Also true for some non-POD classes that have no user declared constructor:

struct A { ~A() { } int a; };
assert(A().a == 0);

由于您不能 A a() (创建一个函数声明),boost有一个类 value_initialized ,允许解决此问题,C ++ 1x将具有以下替代语法

Since you cannot do A a() (creates a function declaration instead), boost has a class value_initialized, allowing to work around that, and C++1x will have the following, alternative, syntax

int a{};

在标准的干燥语言中,这听起来像

In the dry words of the Standard, this sounds like


表达式T(),其中T是非数组完整对象类型或(可能是cv限定的)void类型的简单类型说明符(7.1.5.2) ,创建一个指定类型的右值,它是值初始化的

The expression T(), where T is a simple-type-specifier (7.1.5.2) for a non-array complete object type or the (possibly cv-qualified) void type, creates an rvalue of the specified type, which is value-initialized

由于typedef名是一个类型名,一个简单类型说明符本身,这工作很好。

Since a typedef-name is a type-name, which is a simple-type-specifier itself, this works just fine.

这篇关于C ++做一个POD typedef的值初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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