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

      2. 为什么没有从指针到引用到 const 指针的隐式转换

        why no implicit conversion from pointer to reference to const pointer(为什么没有从指针到引用到 const 指针的隐式转换)

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

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

              • <bdo id='an1fX'></bdo><ul id='an1fX'></ul>

                    <tbody id='an1fX'></tbody>

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

                1. 本文介绍了为什么没有从指针到引用到 const 指针的隐式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我将用代码说明我的问题:

                  I'll illustrate my question with code:

                  #include <iostream>
                  
                  void PrintInt(const unsigned char*& ptr)
                  {
                      int data = 0;
                      ::memcpy(&data, ptr, sizeof(data));
                      // advance the pointer reference.
                      ptr += sizeof(data);
                      std::cout << std::hex << data << " " << std::endl;
                  }
                  
                  int main(int, char**)
                  {
                      unsigned char buffer[] = { 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, };
                  
                      /* const */ unsigned char* ptr = buffer;
                  
                      PrintInt(ptr);  // error C2664: ...
                      PrintInt(ptr);  // error C2664: ...    
                  
                      return 0;
                  }
                  

                  当我运行这段代码(在 VS2008 中)时,我得到这个:错误 C2664:'PrintInt':无法将参数 1 从 'unsigned char *' 转换为 'const unsigned char *&'.如果我取消注释const"注释,它就可以正常工作.

                  When I run this code (in VS2008) I get this: error C2664: 'PrintInt' : cannot convert parameter 1 from 'unsigned char *' to 'const unsigned char *&'. If I uncomment the "const" comment it works fine.

                  但是指针不应该隐式转换为const指针然后引用吗?我期望这行得通有错吗?谢谢!

                  However shouldn't pointer implicitly convert into const pointer and then reference be taken? Am I wrong in expecting this to work? Thanks!

                  推荐答案

                  如果指针被转换为 const 指针,如您所建议的,那么转换的结果是一个临时值,一个 rvalue.您不能将非常量引用附加到右值 - 这在 C++ 中是非法的.

                  If the pointer gets converted to a const pointer, as you suggest, then the result of that conversion is a temporary value, an rvalue. You cannot attach a non-const reference to an rvalue - it is illegal in C++.

                  例如,由于类似的原因,此代码将无法编译

                  For example, this code will not compile for a similar reason

                  int i = 42;
                  double &r = i;
                  

                  即使 int 类型可以转换为 double 类型,它仍然不意味着你可以附加一个 double & 引用到该转换的结果.

                  Even though type int is convertible to type double, it still doesn't mean that you can attach a double & reference to the result of that conversion.

                  然而,const 引用(即引用到const 类型的引用)可以附加到右值,这意味着这段代码可以完美地编译

                  However, a const reference (i.e. a reference of reference-to-const type) can be attached to an rvalue, meaning that this code will compile perfectly fine

                  int i = 42;
                  const double &r = i;
                  

                  在您的情况下,如果您将函数声明为

                  In your case if you declare your function as

                  void PrintInt(const unsigned char* const& ptr) // note the extra `const`
                  

                  代码将被编译.

                  这篇关于为什么没有从指针到引用到 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++ 引用发生变化)

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

                2. <legend id='N4FuY'><style id='N4FuY'><dir id='N4FuY'><q id='N4FuY'></q></dir></style></legend>
                    <bdo id='N4FuY'></bdo><ul id='N4FuY'></ul>

                    1. <tfoot id='N4FuY'></tfoot>
                        <tbody id='N4FuY'></tbody>

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