std::map 内置类型的默认值

发布于 2024-10-09 08:49:00 字数 373 浏览 11 评论 0原文

最近,我对 std::map operator[] 函数感到困惑。 在 MSDN 库中,它说:“如果未找到参数键值,则将其与数据类型的默认值一起插入。” 我试图为这个问题寻找更准确的解释。例如这里: std::map 默认值 在这个页面中,Michael Anderson 说“默认值是由默认构造函数(零参数构造函数)构造的”。

现在我的任务是:“内置类型的默认值是多少?”。与编译器有关吗?或者c++标准委员会是否有针对这个问题的标准?

我在Visual Studio 2008上对“int”类型进行了测试,发现“int”类型被构造为值0。

Recently, I was confused by the std::map operator[] function.
In the MSDN library, it says: "If the argument key value is not found, then it is inserted along with the default value of the data type."
I tryed to search much more exactly explanation for this issue. For example here:
std::map default value
In this page, Michael Anderson said that "the default value is constructed by the default constructor(zero parameter constructor)".

Now my quest comes to this:"what the default value for the build-in type?". Was it compiler related? Or is there a standard for this issue by the c++ stardard committee?

I did a test on visual studio 2008 for the "int" type, and found the "int" type is construted with the value 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梅窗月明清似水 2024-10-16 08:49:00

这是标准中定义的,是的。在这种情况下,地图正在执行“默认初始化”。正如您所说,对于类类型,它调用无参数构造函数。

对于内置类型,在 '98 标准中,请参阅第 8.5 节“初始化程序”:

默认初始化 T 类型的对象意味着:

  • 如果 T 是非 POD ...
  • 如果 T 是数组类型...
  • 否则,对象的存储空间初始化为零

并且之前,

将 T 类型对象的存储空间初始化为零意味着:

  • 如果 T 是标量类型,则存储设置为转换为 T 的值 0(零)

标量类型有:

  • 算术类型(整数、浮点)
  • 枚举类型
  • 指针类型
  • 指向成员类型的指针

特别是,您看到的整数(初始化为零)的行为是由标准定义的,您可以依赖它。

This is defined in the standard, yes. map is performing "default initialization" in this case. As you say, for class types, that calls a no-arguments constructor.

For built-in types, in the '98 standard, see section 8.5, "Initializers":

To default-initialize an object of type T means:

  • if T is a non-POD ...
  • if T is an array type ...
  • otherwise, the storage for the object is zero-initialized

And, previously,

To zero-initialize storage for an object of type T means:

  • if T is a scalar type, the storage is set to the value 0 (zero) converted to T

Scalar types are:

  • Arithmetic types (integer, floating point)
  • Enumeration types
  • Pointer types
  • Pointer to member types

In particular, the behaviour you see with an integer (initialized to zero) is defined by the standard, and you can rely on it.

相思碎 2024-10-16 08:49:00

C++11 标准仍然要求 std::map 对内置类型进行零初始化(与之前的标准一样),但原因与 Luke Halliwell 的答案中的有些不同。特别是,“默认初始化”内置数据类型在 C++11 标准中并不意味着零初始化,而是意味着“不执行任何操作”。 std::map::operator[] 中实际发生的是“值初始化”。

尽管如此,新标准的最终结果与路加的回答是一样的。这些值将被初始化为零。以下是标准的相关部分:

第 23.4.4.3 节“映射元素访问”说

T&运算符[](const key_type&x);

效果:如果映射中没有与 x 等效的键,则将 value_type(x, T()) 插入到映射中。

...

表达式 T() 在第 8.5 节中描述

如果对象的初始值设定项是一组空括号(即 ()),则应值初始化

X a();

这种“值初始化”在同一节中进行了描述

对 T 类型的对象进行值初始化意味着:

  • 如果 T 是一个(可能是 cv 限定的)类类型(第 9 条),具有用户提供的构造函数 (12.1),则 T 的默认构造函数
    被调用(如果 T 没有可访问的,则初始化是错误的
    默认构造函数);
  • 如果 T 是一个(可能是 cv 限定的)非联合类类型,没有用户提供的构造函数,则该对象为零初始化,并且,如果
    T 的隐式声明的默认构造函数并不简单,即
    调用构造函数。
  • 如果 T 是数组类型,则每个元素都进行值初始化;
  • 否则,该对象将被零初始化。

The C++11 standard still requires that std::map zero-initializes built in types (as did the previous standard), but the reasons are a bit different to those in Luke Halliwell's answer. In particular, to 'default-initialize' a built-in data type doesn't mean zero-initialize in the C++11 standard, but rather it would mean 'do nothing'. What actually happens in std::map::operator[] is a 'value-initialization'.

Nevertheless, the end result in the new standard is the same as in Luke's answer. The values will be zero-initialized. Here are the relevant parts of the standard:

Section 23.4.4.3 "map element access" says

T& operator[](const key_type& x);

Effects: If there is no key equivalent to x in the map, inserts value_type(x, T()) into the map.

...

The expression T() is described in section 8.5

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

X a();

And this kind of 'value-initialization' is described in the same section

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T
    is called (and the initialization is ill-formed if T has no accessible
    default constructor);
  • if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if
    T’s implicitly-declared default constructor is non-trivial, that
    constructor is called.
  • if T is an array type, then each element is value-initialized;
  • otherwise, the object is zero-initialized.
痴梦一场 2024-10-16 08:49:00

类类型对象的默认值是由类的默认构造函数设置的。对于内置类型,默认值为 0。

但请注意,未初始化的内置变量与初始化为其默认值的内置变量之间存在差异。未初始化的内置函数可能会保存当时该变量内存地址中的任何值。

int i;          // i has an arbitrary undefined value
int x = int();  // x is 0

The default value of class-type objects is that set by the default constructor of the class. For built-in types the default value is 0.

But note that there is a difference between a built-in variable that isn't initialized, and one initialized to its default value. A built-in that is not initialized will probably hold whatever value was in that variable's memory address at the time.

int i;          // i has an arbitrary undefined value
int x = int();  // x is 0
把回忆走一遍 2024-10-16 08:49:00
 |expression:   | POD type T                               | non-POD type T
 ==================================================================================================
 | new T         | not initialized                          | default-initialized
 | new T()       | always default-initialized               | always default-initialized
 | new T(x)      | always initialized via a constructor     | always initialized via a constructor

据我所知,stl使用new T()作为默认值,因此如果int为0,它将被默认初始化。

 |expression:   | POD type T                               | non-POD type T
 ==================================================================================================
 | new T         | not initialized                          | default-initialized
 | new T()       | always default-initialized               | always default-initialized
 | new T(x)      | always initialized via a constructor     | always initialized via a constructor

As far as i know, stl uses new T() for default values, so it will be default-initialized, in case of int to 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文