std::map 内置类型的默认值
最近,我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是标准中定义的,是的。在这种情况下,地图正在执行“默认初始化”。正如您所说,对于类类型,它调用无参数构造函数。
对于内置类型,在 '98 标准中,请参阅第 8.5 节“初始化程序”:
并且之前,
标量类型有:
特别是,您看到的整数(初始化为零)的行为是由标准定义的,您可以依赖它。
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":
And, previously,
Scalar types are:
In particular, the behaviour you see with an integer (initialized to zero) is defined by the standard, and you can rely on it.
C++11 标准仍然要求 std::map 对内置类型进行零初始化(与之前的标准一样),但原因与 Luke Halliwell 的答案中的有些不同。特别是,“默认初始化”内置数据类型在 C++11 标准中并不意味着零初始化,而是意味着“不执行任何操作”。
std::map::operator[]
中实际发生的是“值初始化”。尽管如此,新标准的最终结果与路加的回答是一样的。这些值将被初始化为零。以下是标准的相关部分:
第 23.4.4.3 节“映射元素访问”说
表达式
T()
在第 8.5 节中描述这种“值初始化”在同一节中进行了描述
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
The expression
T()
is described in section 8.5And this kind of 'value-initialization' is described in the same section
类类型对象的默认值是由类的默认构造函数设置的。对于内置类型,默认值为 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.
据我所知,stl使用new T()作为默认值,因此如果int为0,它将被默认初始化。
As far as i know, stl uses new T() for default values, so it will be default-initialized, in case of int to 0.