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

问题描述

C ++在简单的 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有一个类,允许解决此问题,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

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

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

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

10-29 07:07