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

    <small id='aSxgW'></small><noframes id='aSxgW'>

    <legend id='aSxgW'><style id='aSxgW'><dir id='aSxgW'><q id='aSxgW'></q></dir></style></legend>

      1. <tfoot id='aSxgW'></tfoot>
          <bdo id='aSxgW'></bdo><ul id='aSxgW'></ul>

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

        VC++ allows to use const types for STL containers. Why?(VC++ 允许对 STL 容器使用 const 类型.为什么?)
          <legend id='7alwF'><style id='7alwF'><dir id='7alwF'><q id='7alwF'></q></dir></style></legend>
          <tfoot id='7alwF'></tfoot>
            <tbody id='7alwF'></tbody>
            • <bdo id='7alwF'></bdo><ul id='7alwF'></ul>
                <i id='7alwF'><tr id='7alwF'><dt id='7alwF'><q id='7alwF'><span id='7alwF'><b id='7alwF'><form id='7alwF'><ins id='7alwF'></ins><ul id='7alwF'></ul><sub id='7alwF'></sub></form><legend id='7alwF'></legend><bdo id='7alwF'><pre id='7alwF'><center id='7alwF'></center></pre></bdo></b><th id='7alwF'></th></span></q></dt></tr></i><div id='7alwF'><tfoot id='7alwF'></tfoot><dl id='7alwF'><fieldset id='7alwF'></fieldset></dl></div>

                <small id='7alwF'></small><noframes id='7alwF'>

                1. 本文介绍了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 类型.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                    <bdo id='wEoES'></bdo><ul id='wEoES'></ul>

                    <tfoot id='wEoES'></tfoot>

                    <small id='wEoES'></small><noframes id='wEoES'>

                      <tbody id='wEoES'></tbody>

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

                            <legend id='wEoES'><style id='wEoES'><dir id='wEoES'><q id='wEoES'></q></dir></style></legend>