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

      • <bdo id='KVeoP'></bdo><ul id='KVeoP'></ul>
      <tfoot id='KVeoP'></tfoot>
        <i id='KVeoP'><tr id='KVeoP'><dt id='KVeoP'><q id='KVeoP'><span id='KVeoP'><b id='KVeoP'><form id='KVeoP'><ins id='KVeoP'></ins><ul id='KVeoP'></ul><sub id='KVeoP'></sub></form><legend id='KVeoP'></legend><bdo id='KVeoP'><pre id='KVeoP'><center id='KVeoP'></center></pre></bdo></b><th id='KVeoP'></th></span></q></dt></tr></i><div id='KVeoP'><tfoot id='KVeoP'></tfoot><dl id='KVeoP'><fieldset id='KVeoP'></fieldset></dl></div>
        <legend id='KVeoP'><style id='KVeoP'><dir id='KVeoP'><q id='KVeoP'></q></dir></style></legend>
      1. 我说 const_cast 然后修改绑定到临时对象的 ref-to-const 是对的吗?

        Am I right in saying that const_cast followed by modification on a ref-to-const bound to a temporary is okay?(我说 const_cast 然后修改绑定到临时对象的 ref-to-const 是对的吗?)
        • <bdo id='JmvSG'></bdo><ul id='JmvSG'></ul>

              <tfoot id='JmvSG'></tfoot>

                <tbody id='JmvSG'></tbody>
                1. <legend id='JmvSG'><style id='JmvSG'><dir id='JmvSG'><q id='JmvSG'></q></dir></style></legend>
                2. <small id='JmvSG'></small><noframes id='JmvSG'>

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

                  本文介绍了我说 const_cast 然后修改绑定到临时对象的 ref-to-const 是对的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!



                  I would like to check my understanding and conclusions on this matter.

                  在 IRC 上,有人问:

                  On IRC, it was asked:

                  const_cast 是否可以接受绑定到临时对象的 const 引用?

                  Is it acceptable to const_cast a const reference that's bound to a temporary object?

                  翻译:他有一个 ref-to-const 绑定到一个临时对象,他想抛弃它的 const-ness 来修改它.

                  Translating: he has a ref-to-const bound to a temporary, and he wants to cast away its const-ness to modify it.

                  我的回答是我问了a之前类似的问题,其中的共识似乎是临时变量本身并不是固有的 const,因此您可以摆脱引用的 const-ness你必须对它们,并通过结果修改它们.而且,只要那个原始的 ref-to-const 仍然存在,这不会影响临时的生命周期.

                  My response was that I'd asked a similar question previously, where the consensus seemed to be that temporaries themselves are not inherently const, and thus that you can cast off the const-ness of a reference you have to them, and modify them through the result. And, as long as that original ref-to-const still exists, this won't affect the temporary's lifetime.


                  int main()
                     const int& x = int(3);
                     int& y = const_cast<int&>(x);
                     y = 4;
                     cout << x;
                  // Output: 4
                  // ^ Legal and safe





                  首先,据我所知,它是否是文字是无关.非类类型的右值总是有非 cv 限定类型(第 3.10/9 节),但是,在第 8.5.3 节(引用的初始化)中,我们有:

                  First, as far as I can tell, whether it is a literal or not is irrelevant. Rvalues of non-class types always have non-cv qualified types (§3.10/9), however, in §8.5.3 (initialization of a reference), we have:

                  对cv1 T1"类型的引用由cv2 T2"类型的表达式初始化,如下所示:

                  A reference to type "cv1 T1" is initialized by an expression of type "cv2 T2" as follows:



                  否则,将使用非引用复制初始化规则 (8.5) 从初始化表达式创建和初始化类型为cv1 T1"的临时对象.然后将引用绑定到临时对象.如果 T1 与 T2 引用相关,则 cv1 必须与 cv-qualification 相同或更高比,cv2;否则,程序格式错误.

                  Otherwise, a temporary of type "cv1 T1" is created and initialized from the initializer expression using the rules for a non-reference copy initialization (8.5). The reference is then bound to the temporary. If T1 is reference-related to T2, cv1 must be the same cv-qualification as, or greater cvqualification than, cv2; otherwise, the program is ill-formed.


                  (All of the preceding points concern either lvalues or class types.)


                  In our case, we have:

                  int const& x = ...;

                  所以cv1 T1int const,我们创建的临时对象有类型int const.这是一个顶级常量(在对象上),所以任何尝试修改它是未定义的行为.

                  So cv1 T1 is int const, and the temporary object we create has type int const. This is a top level const (on the object), so any attempt to modify it is undefined behavior.


                  At least, that's my interpretation. I wish the standard were a bit clearer about this.

                  这篇关于我说 const_cast 然后修改绑定到临时对象的 ref-to-const 是对的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                          <tbody id='qdlc9'></tbody>
                        <tfoot id='qdlc9'></tfoot>
                        • <i id='qdlc9'><tr id='qdlc9'><dt id='qdlc9'><q id='qdlc9'><span id='qdlc9'><b id='qdlc9'><form id='qdlc9'><ins id='qdlc9'></ins><ul id='qdlc9'></ul><sub id='qdlc9'></sub></form><legend id='qdlc9'></legend><bdo id='qdlc9'><pre id='qdlc9'><center id='qdlc9'></center></pre></bdo></b><th id='qdlc9'></th></span></q></dt></tr></i><div id='qdlc9'><tfoot id='qdlc9'></tfoot><dl id='qdlc9'><fieldset id='qdlc9'></fieldset></dl></div>

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

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

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