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

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

        正在铸造 std::pair&lt;T1, T2&gt;常量&amp;到

        时间:2023-05-24
        <legend id='uQAb7'><style id='uQAb7'><dir id='uQAb7'><q id='uQAb7'></q></dir></style></legend>

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

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

                  本文介绍了正在铸造 std::pair&lt;T1, T2&gt;常量&amp;到 std::pair<T1 const, T2>常量&amp;安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  reinterpret_cast std::pair 是否安全(理论上或实践中)?const & 转换为 std::pairconst &,假设程序员没有故意做一些奇怪的事情,比如专门化 std::pair?

                  Is it safe (in theory or in practice) to reinterpret_cast a std::pair<T1, T2> const & into a std::pair<T1 const, T2> const &, assuming that the programmer hasn't intentionally done something weird like specializing std::pair<T1 const, T2>?

                  推荐答案

                  这样做不可移植.

                  std::pair 要求在第 20.3 条中列出.第 17.5.2.3 条阐明

                  std::pair requirements are laid out in clause 20.3. Clause 17.5.2.3 clarifies that

                  第 18 条到第 30 条和附件 D 没有指定类的表示,并有意省略了类成员的说明.一个实现可以根据需要定义静态或非静态类成员,或两者都定义,以实现第 18 至 30 条和附件 D 中指定的成员函数的语义.

                  Clauses 18 through 30 and Annex D do not specify the representation of classes, and intentionally omit specification of class members. An implementation may define static or non-static class members, or both, as needed to implement the semantics of the member functions specified in Clauses 18 through 30 and Annex D.

                  这意味着实现包含部分特化是合法的(尽管不太可能),例如:

                  This implies that it's legal (although incredibly unlikely) for an implementation to include a partial specialization such as:

                  template<typename T1, typename T2>
                  struct pair<T1, T2>
                  {
                      T1 first;
                      T2 second;
                  };
                  
                  template<typename T1, typename T2>
                  struct pair<const T1, T2>
                  {
                      T2 second;
                      const T1 first;
                  };
                  

                  显然与布局不兼容.该规则还允许其他变体,包括可能在 first 和/或 second 之前包含额外的非静态数据成员.

                  which are clearly not layout-compatible. Other variations including inclusion of additional non-static data members possibly before first and/or second are also allowed under the rule.

                  现在,考虑布局已知的情况有点有趣.尽管 Potatoswatter 指出 DR1334 断言 Tconst T 不是 layout-compatible,标准提供了足够的保证,让我们无论如何都能获得大部分方式:

                  Now, it is somewhat interesting to consider the case where the layout is known. Although Potatoswatter pointed out DR1334 which asserts that T and const T are not layout-compatible, the Standard provides enough guarantees to allow us to get most of the way anyway:

                  template<typename T1, typename T2>
                  struct mypair<T1, T2>
                  {
                      T1 first;
                      T2 second;
                  };
                  
                  mypair<int, double> pair1;
                  mypair<int, double>* p1 = &pair1;
                  int* p2 = reinterpret_cast<int*>(p1); // legal by 9.2p20
                  const int* p3 = p2;
                  mypair<const int, double>* p4 = reinterpret_cast<mypair<const int, double>*>(p3); // again 9.2p20
                  

                  然而这不适用于 std::pair 因为我们不能在不知道 first 实际上是未指定的初始成员的情况下应用 9.2p20.

                  However this doesn't work on std::pair as we can't apply 9.2p20 without knowing that first is actually the initial member, which is not specified.

                  这篇关于正在铸造 std::pair&lt;T1, T2&gt;常量&amp;到 std::pair<T1 const, T2>常量&amp;安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:有没有办法在 VS2012 中编写 make_unique() ? 下一篇:const 和非 const 模板特化

                  相关文章

                  最新文章

                1. <tfoot id='BJwSl'></tfoot>

                  1. <small id='BJwSl'></small><noframes id='BJwSl'>

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

                    1. <legend id='BJwSl'><style id='BJwSl'><dir id='BJwSl'><q id='BJwSl'></q></dir></style></legend>
                        <bdo id='BJwSl'></bdo><ul id='BJwSl'></ul>