1. <tfoot id='6LF7j'></tfoot>
      <legend id='6LF7j'><style id='6LF7j'><dir id='6LF7j'><q id='6LF7j'></q></dir></style></legend>
      <i id='6LF7j'><tr id='6LF7j'><dt id='6LF7j'><q id='6LF7j'><span id='6LF7j'><b id='6LF7j'><form id='6LF7j'><ins id='6LF7j'></ins><ul id='6LF7j'></ul><sub id='6LF7j'></sub></form><legend id='6LF7j'></legend><bdo id='6LF7j'><pre id='6LF7j'><center id='6LF7j'></center></pre></bdo></b><th id='6LF7j'></th></span></q></dt></tr></i><div id='6LF7j'><tfoot id='6LF7j'></tfoot><dl id='6LF7j'><fieldset id='6LF7j'></fieldset></dl></div>
        <bdo id='6LF7j'></bdo><ul id='6LF7j'></ul>
    2. <small id='6LF7j'></small><noframes id='6LF7j'>

        VC++ 允许对 STL 容器使用 const 类型.为什么?

        时间:2023-10-06

          <tfoot id='sx3hx'></tfoot>
            <tbody id='sx3hx'></tbody>
          <i id='sx3hx'><tr id='sx3hx'><dt id='sx3hx'><q id='sx3hx'><span id='sx3hx'><b id='sx3hx'><form id='sx3hx'><ins id='sx3hx'></ins><ul id='sx3hx'></ul><sub id='sx3hx'></sub></form><legend id='sx3hx'></legend><bdo id='sx3hx'><pre id='sx3hx'><center id='sx3hx'></center></pre></bdo></b><th id='sx3hx'></th></span></q></dt></tr></i><div id='sx3hx'><tfoot id='sx3hx'></tfoot><dl id='sx3hx'><fieldset id='sx3hx'></fieldset></dl></div>

          • <legend id='sx3hx'><style id='sx3hx'><dir id='sx3hx'><q id='sx3hx'></q></dir></style></legend>

              <bdo id='sx3hx'></bdo><ul id='sx3hx'></ul>
            • <small id='sx3hx'></small><noframes id='sx3hx'>

                • 本文介绍了VC++ 允许对 STL 容器使用 const 类型.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  STL 容器要求存储的值是可复制构造和可分配的.const T 显然不是任何 T 的可赋值类型,但我尝试使用它(只是出于好奇)并发现它可以编译,而且还表现为可赋值类型.

                  STL containers require the stored values to be copy constructible and assignable. const T is obviously not an assignable type for any T, but I tried to use it (just being curious) and found out that it compiles and, moreover, behaves as an assignable type.

                  vector<const int> v(1);
                  v[0] = 17;
                  

                  这在 Visual Studio 2008 中成功运行并将 v[0] 分配给 17.

                  This successfully runs in Visual Studio 2008 and assigns v[0] to 17.

                  推荐答案

                  这不是其他人建议的实现中的错误.

                  This is not a bug in the implementation as others have suggested.

                  违反 C++ 标准库工具的要求不会使您的程序格式错误,而是会产生未定义的行为.

                  Violating the requirements of a C++ Standard Library facility does not render your program ill-formed, it yields undefined behavior.

                  您违反了存储在容器中的值类型必须是可复制构造和可分配的要求(const 类型显然不可分配),因此您的程序表现出未定义的行为.

                  You have violated the requirement that the value type stored in a container must be copy constructible and assignable (const types are not assignable, obviously), so your program exhibits undefined behavior.

                  C++ 标准中的适用语言可以在 C++03 17.4.3.6 [lib.res.on.functions] 中找到:

                  The applicable language from the C++ Standard can be found in C++03 17.4.3.6 [lib.res.on.functions]:

                  在某些情况下(替换函数、处理函数、对用于实例化标准库模板组件的类型的操作),C++ 标准库依赖于由 C++ 程序提供的组件.如果这些组件不满足他们的要求,则标准不会对实现提出要求.

                  In certain cases (replacement functions, handler functions, operations on types used to instantiate standard library template components), the C++ Standard Library depends on components supplied by a C++ program. If these components do not meet their requirements, the Standard places no requirements on the implementation.

                  特别是,在以下情况下效果未定义:

                  In particular, the effects are undefined in the following cases:

                  ...

                  • 对于在实例化模板组件时用作模板参数的类型,如果对类型的操作没有实现适用的需求子条款的语义.

                  Visual C++ 标准库实现可以对这段代码做任何事情,包括静默删除或忽略 const 限定,它仍然符合标准.

                  The Visual C++ Standard Library implementation may do anything with this code, including silently removing or ignoring the const-qualification, and it is still standards-conforming.

                  这篇关于VC++ 允许对 STL 容器使用 const 类型.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 extern c 和 dllexport 与模块定义 (msvc++) 进行 下一篇:错误:命令“C:\ Visual Studio\2017\Community\VC\Tool

                  相关文章

                  最新文章

                  <legend id='qJaXu'><style id='qJaXu'><dir id='qJaXu'><q id='qJaXu'></q></dir></style></legend>
                • <i id='qJaXu'><tr id='qJaXu'><dt id='qJaXu'><q id='qJaXu'><span id='qJaXu'><b id='qJaXu'><form id='qJaXu'><ins id='qJaXu'></ins><ul id='qJaXu'></ul><sub id='qJaXu'></sub></form><legend id='qJaXu'></legend><bdo id='qJaXu'><pre id='qJaXu'><center id='qJaXu'></center></pre></bdo></b><th id='qJaXu'></th></span></q></dt></tr></i><div id='qJaXu'><tfoot id='qJaXu'></tfoot><dl id='qJaXu'><fieldset id='qJaXu'></fieldset></dl></div>
                    <bdo id='qJaXu'></bdo><ul id='qJaXu'></ul>

                    1. <tfoot id='qJaXu'></tfoot>
                    2. <small id='qJaXu'></small><noframes id='qJaXu'>