• <legend id='yxTlG'><style id='yxTlG'><dir id='yxTlG'><q id='yxTlG'></q></dir></style></legend>
  • <small id='yxTlG'></small><noframes id='yxTlG'>

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

      1. 关于 C++ 中类型双关的观点?

        Opinions on type-punning in C++?(关于 C++ 中类型双关的观点?)

        <legend id='kOBTj'><style id='kOBTj'><dir id='kOBTj'><q id='kOBTj'></q></dir></style></legend>
          <tbody id='kOBTj'></tbody>

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

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

                <bdo id='kOBTj'></bdo><ul id='kOBTj'></ul>

                  本文介绍了关于 C++ 中类型双关的观点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 C++ 中类型双关指针/数组的约定很好奇.这是我目前的用例:

                  <块引用>通过将二进制数据块视为 32 位整数数组(我们知道其总长度是 4 的倍数),然后对所有值求和并忽略溢出,计算一个简单的 32 位校验和.

                  我希望这样的函数看起来像这样:

                  uint32_t compute_checksum(const char *data, size_t size){const uint32_t *udata =/* ???*/;uint32_t 校验和 = 0;for (size_t i = 0; i != size/4; ++i)校验和 += udata[i];返回udata;}

                  现在我的问题是,您认为将 data 转换为 udata 的最佳"方法是什么?

                  C 风格的类型转换?

                  udata = (const uint32_t *)data

                  假设所有指针都可以转换的 C++ 类型转换?

                  udata = reinterpret_cast(data)

                  C++ 使用中间 void* 在任意指针类型之间进行转换?

                  udata = static_cast(static_cast(data))

                  通过联合施放?

                  union {const uint32_t *udata;const char *cdata;};cdata = 数据;//现在使用 udata

                  我完全意识到这不是一个 100% 可移植的解决方案,但我只希望在我知道它可以工作的一小组平台上使用它(即未对齐的内存访问和编译器对指针别名的假设).你会推荐什么?

                  解决方案

                  就 C++ 标准而言,litb 的答案是完全正确且最便携的.将 const char *data 转换为 const uint3_t *,无论是通过 C 风格的转换、static_cast 还是 reinterpret_cast,打破了严格的别名规则(参见 了解严格别名).如果您以完全优化的方式编译,则代码很可能不会做正确的事情.

                  通过联合(例如 litb 的 my_reint)进行转换可能是最好的解决方案,尽管它在技术上确实违反了规则,即如果您通过一个成员写入联合并通过另一个成员读取它,它导致未定义的行为.但是,实际上所有编译器都支持这一点,并且会产生预期的结果.如果您绝对希望 100% 符合标准,请使用位移位方法.否则,我建议您通过联合进行强制转换,这可能会给您带来更好的性能.

                  I'm curious about conventions for type-punning pointers/arrays in C++. Here's the use case I have at the moment:

                  Compute a simple 32-bit checksum over a binary blob of data by treating it as an array of 32-bit integers (we know its total length is a multiple of 4), and then summing up all values and ignoring overflow.

                  I would expect such an function to look like this:

                  uint32_t compute_checksum(const char *data, size_t size)
                  {
                      const uint32_t *udata = /* ??? */;
                      uint32_t checksum = 0;
                      for (size_t i = 0; i != size / 4; ++i)
                          checksum += udata[i];
                      return udata;
                   }
                  

                  Now the question I have is, what do you consider the "best" way to convert data to udata?

                  C-style cast?

                  udata = (const uint32_t *)data
                  

                  C++ cast that assumes all pointers are convertible?

                  udata = reinterpret_cast<const uint32_t *>(data)
                  

                  C++ cast that between arbitrary pointer types using intermediate void*?

                  udata = static_cast<const uint32_t *>(static_cast<const void *>(data))
                  

                  Cast through a union?

                  union {
                      const uint32_t *udata;
                      const char *cdata;
                  };
                  cdata = data;
                  // now use udata
                  

                  I fully realize that this will not be a 100% portable solution, but I am only expecting to use it on a small set of platforms where I know it works (namely unaligned memory accesses and compiler assumptions on pointer aliasing). What would you recommend?

                  解决方案

                  As far as the C++ standard is concerned, litb's answer is completely correct and the most portable. Casting const char *data to a const uint3_t *, whether it be via a C-style cast, static_cast, or reinterpret_cast, breaks the strict aliasing rules (see Understanding Strict Aliasing). If you compile with full optimization, there's a good chance that the code will not do the right thing.

                  Casting through a union (such as litb's my_reint) is probably the best solution, although it does technically violate the rule that if you write to a union through one member and read it through another, it results in undefined behavior. However, practically all compilers support this, and it results in the the expected result. If you absolutely desire to conform to the standard 100%, go with the bit-shifting method. Otherwise, I'd recommend going with casting through a union, which is likely to give you better performance.

                  这篇关于关于 C++ 中类型双关的观点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                    <tfoot id='qNv80'></tfoot>
                      • <bdo id='qNv80'></bdo><ul id='qNv80'></ul>

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

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

                            <tbody id='qNv80'></tbody>

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