为什么std :: array没有一个构造函数需要填充数组的值? [英] Why does std::array not have an constructor that takes a value for the array to be filled with?

查看:106
本文介绍了为什么std :: array没有一个构造函数需要填充数组的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否缺少

std::array<T,N>::array(const T& value);

一个疏忽?它对我来说很有用,动态容器(如 std :: vector )也有类似的构造函数。

an oversight? It seems mighty useful to me, and dynamic containers (like std::vector) do have a similar constructor.

我完全知道

std::array<T,N>::fill(const T& value);

但是这不是一个构造函数,并且内存将首先被清零。如果我想让所有 -1

but that is not a constructor, and the memory will be zeroed out first. What if I want all -1's like this guy?

推荐答案

std :: array 是设计的聚合,因此没有用户声明的构造函数。

std::array is, by design, an aggregate, so has no user-declared constructors.

你说,你可以在默认构造后使用 fill 。由于它是一个聚合,默认的构造不会使内存为零,而是将其未初始化(如果包含的类型是简单初始化的)。

As you say, you could use fill after default constructing. Since it's an aggregate, default construction won't zero the memory, but will leave it uninitialised (if the contained type is trivially initialisable).

这篇关于为什么std :: array没有一个构造函数需要填充数组的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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