• <small id='WnLUR'></small><noframes id='WnLUR'>

    <tfoot id='WnLUR'></tfoot><legend id='WnLUR'><style id='WnLUR'><dir id='WnLUR'><q id='WnLUR'></q></dir></style></legend>

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

      1. 强制转换的结果是右值吗?

        时间:2023-07-02
        • <small id='9wn4B'></small><noframes id='9wn4B'>

        • <tfoot id='9wn4B'></tfoot>

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

                1. 本文介绍了强制转换的结果是右值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  int a = 0;

                  那么(int)a是标准C++中的rvalue吗?

                  不同的编译器对此代码显示不同的结果:

                  #include 使用命名空间标准;void f(int& x){cout<<l值"<<结束;}void f(int&& x){cout<<r值"<<结束;}int main(){int a = 0;f((int)a);}

                  不同结果的编译器:

                  1) http://cpp.sh/2r6

                  2) http://webcompiler.cloudapp.net/

                  解决方案

                  The 应该是一个 rvalue 但是 webcompiler 正在运行 Visual Studio 并且 Visual Studio 有一个 允许将临时对象绑定到非常量左值引用的扩展. 一个错误/扩展,导致它在这种情况下生成左值 就像 Igor上面指出这可以使用 /Za 禁用(现场观看).

                  我们可以看到它应该是来自 C++ 标准草案部分 5.4 显式类型的 rvalue(特别是 prvalue)转换(转换符号) 段落 1 表示(强调我的):

                  <块引用>

                  表达式 (T) cast-expression 的结果是 T 类型.如果 T 是左值引用类型或右值,则结果是左值如果 T 是右值引用,则引用函数类型和 xvalue对象类型;否则结果是纯右值.[ 注意:如果 T 是具有 cv 限定的非类类型,忽略 cv 限定符在确定结果纯右值的类型时;见 3.10.-结尾注意]

                  gcc 和 clang 结果在 rvalue 这是预期的结果.

                  顺便说一句,我建议使用 rextester 而不是 webcompiler,因为 rextester 允许您分享您的节目,也有现场分享.

                  更新

                  Ben Voigt 指出了这个错误报告,因此似乎 VisualStudio 实际上产生一个左值.因此,这不仅仅是 允许将临时对象绑定到非常量左值引用的扩展的情况.>

                  正如 dyp 指出的那样 gcc 曾经有一个转换为左值扩展

                  更新 2

                  Mgetz 提交了错误报告,得到的答复是已修复通过使用 /Zc:rvalueCast 标志,标志的描述如下:

                  <块引用>

                  当指定了/Zc:rvalueCast 选项时,编译器正确将右值引用类型标识为强制转换操作的结果符合C++11标准.When the option is not指定,编译器行为与 Visual Studio 2012 中的相同.默认情况下,/Zc:rvalueCast 是关闭的.为了一致性和消除使用强制转换的错误,我们建议您使用/Zc:rvalueCast.

                  尚不清楚在未来版本中是否会默认启用此标志.

                  Let

                  int a = 0;
                  

                  Then is (int)a an rvalue in standard C++?

                  Different compilers show different results for this code:

                  #include <iostream>
                  using namespace std;
                  
                  void f(int& x)
                  {
                      cout << "l value" << endl;
                  }
                  
                  void f(int&& x)
                  {
                      cout << "r value" << endl;
                  }
                  
                  int main()
                  {
                      int a = 0;
                      f((int)a);
                  }
                  

                  compilers with different results:

                  1) http://cpp.sh/2r6

                  2) http://webcompiler.cloudapp.net/

                  解决方案

                  The should be an rvalue but webcompiler is running Visual Studio and Visual Studio has an extension which allows temporary objects to be bound to non-const lvalue references. a bug/extension that casues it to generate an lvalue in this case As Igor points out above this can be disabled using /Za (see it live).

                  We can see that it should be an rvalue(specifically a prvalue) from the draft C++ standard section 5.4 Explicit type conversion (cast notation) paragraph 1 which says (emphasis mine):

                  The result of the expression (T) cast-expression is of type T. The result is an lvalue if T is an lvalue reference type or an rvalue reference to function type and an xvalue if T is an rvalue reference to object type; otherwise the result is a prvalue.[ Note: if T is a non-class type that is cv-qualified, the cv-qualifiers are ignored when determining the type of the resulting prvalue; see 3.10. —end note ]

                  Both gcc and clang result in rvalue which is the expected result.

                  As an aside, I would recommend using rextester over webcompiler since rextester allows you to share your program and also has live sharing.

                  Update

                  Ben Voigt point out this bug report and so it seems that Visual Studio actually produces an lvalue. So this is not simply a case of the extension which allows temporary objects to be bound to non-const lvalue references.

                  As dyp points out gcc used to have a cast to lvalue extension as well.

                  Update 2

                  Mgetz filed a bug report, the response was that this is fixed by using the /Zc:rvalueCast flag, the description of the flag is as follows:

                  When the /Zc:rvalueCast option is specified, the compiler correctly identifies an rvalue reference type as the result of a cast operation in accordance with the C++11 standard. When the option is not specified, the compiler behavior is the same as in Visual Studio 2012. By default, /Zc:rvalueCast is off. For conformance and to eliminate errors in the use of casts, we recommend that you use /Zc:rvalueCast.

                  It is unclear whether this flag will be enabled by default in future versions.

                  这篇关于强制转换的结果是右值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 Visual C++ 在 C 中对从 const void ** 到 void * 的隐式转换发出警告,但在 C 下一篇:IsBadReadPtr 的最有效替代品?

                  相关文章

                2. <legend id='W826r'><style id='W826r'><dir id='W826r'><q id='W826r'></q></dir></style></legend>

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

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

                      <tfoot id='W826r'></tfoot>