• <legend id='ZJk9g'><style id='ZJk9g'><dir id='ZJk9g'><q id='ZJk9g'></q></dir></style></legend>
  • <tfoot id='ZJk9g'></tfoot>

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

        大小小于 int 的位域是否应该作为积分提升的主题?

        Should bit-fields less than int in size be the subject of integral promotion?(大小小于 int 的位域是否应该作为积分提升的主题?)
          <tbody id='QI4HE'></tbody>

      2. <tfoot id='QI4HE'></tfoot>

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

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

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

                  本文介绍了大小小于 int 的位域是否应该作为积分提升的主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有以下 struct:

                  struct A
                  {
                      unsigned int a : 1;
                      unsigned int b : 1;
                  };
                  

                  我感兴趣的是表达式a + b 的类型.虽然从技术上讲位域具有小于 int 的类型",因此可能应该发生积分提升,然后结果是 int 就像它恰好在 gcc 和 clang 中一样.

                  What interests me is the type of expression a + b. While technically bit-fields have "type" with size less than int so integral promotion probably should happen and then result is int like it happens to be in gcc and clang.

                  但是由于不可能提取位字段本身的确切类型,并且它总是会被推导出它的大"类型(即 unsigned int 在这种情况下)是否正确积分推广应该发生吗?因为我们实际上不能谈论位域的确切类型和它们的大小,除非它们被推导出为 unsigned int 在这种情况下不应该发生积分提升.

                  But since it's impossible to extract the exact type of bit-field itself and it will always be deduced to be its "big" type (i.e. unsigned int in this case) is it correct that integral promotion should happen? Because we can't actually talk about exact types and their sizes for bit-fields except them being deduced as unsigned int in which case integral promotion shouldn't happen.

                  (我的问题再次源于这样一个事实,即 MSVC 恰好认为 unsigned int 是这种表达式的类型)

                  (Once again my question stems from the fact that MSVC happens to think that unsigned int is type of such expression)

                  推荐答案

                  如果我们去draft C++ 标准:N4140 部分 5 它说:

                  If we go to the draft C++ standard: N4140 section 5 it says:

                  许多需要算术或运算操作数的二元运算符枚举类型导致转换并产生类似的结果类型大大地.目的是产生一个通用类型,它也是结果.这种模式称为通常的算术转换,定义如下

                  Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows

                  并且以下要点适用:

                  • 否则,应在两个操作数上执行整数提升 (4.5).然后,将以下规则应用于提升的操作数操作数:

                  和第 4.5 节说(强调我的):

                  and section 4.5 which says (emphasis mine):

                  整数位域 (9.6) 的纯右值可以转换为类型 int 的纯右值如果 int 可以表示位域; 否则,如果是无符号的,则可以转换为无符号整数int 可以表示位域的所有值.如果位域是更大,没有积分促销适用于它.如果位域有枚举类型,它被视为该类型的任何其他值推广目的.

                  A prvalue for an integral bit-field (9.6) can be converted to a prvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field. If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.

                  所以gcc和clang是正确的,ab应该提升为int.

                  So gcc and clang are correct, a and b should be promoted to int.

                  这篇关于大小小于 int 的位域是否应该作为积分提升的主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='9ip0O'></tfoot>
                    <legend id='9ip0O'><style id='9ip0O'><dir id='9ip0O'><q id='9ip0O'></q></dir></style></legend>

                      <bdo id='9ip0O'></bdo><ul id='9ip0O'></ul>
                        • <small id='9ip0O'></small><noframes id='9ip0O'>

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