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

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

          <bdo id='aOVaU'></bdo><ul id='aOVaU'></ul>
      1. std::make_pair 与 C++ 11

        时间:2023-07-19

            <tbody id='mu6Ey'></tbody>
            <bdo id='mu6Ey'></bdo><ul id='mu6Ey'></ul>
            <legend id='mu6Ey'><style id='mu6Ey'><dir id='mu6Ey'><q id='mu6Ey'></q></dir></style></legend>

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

              <tfoot id='mu6Ey'></tfoot>
            1. <i id='mu6Ey'><tr id='mu6Ey'><dt id='mu6Ey'><q id='mu6Ey'><span id='mu6Ey'><b id='mu6Ey'><form id='mu6Ey'><ins id='mu6Ey'></ins><ul id='mu6Ey'></ul><sub id='mu6Ey'></sub></form><legend id='mu6Ey'></legend><bdo id='mu6Ey'><pre id='mu6Ey'><center id='mu6Ey'></center></pre></bdo></b><th id='mu6Ey'></th></span></q></dt></tr></i><div id='mu6Ey'><tfoot id='mu6Ey'></tfoot><dl id='mu6Ey'><fieldset id='mu6Ey'></fieldset></dl></div>
                  本文介绍了std::make_pair 与 C++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您好,我有以下代码:

                  bool PinManager::insertPin(const std::string& p_pinNumber, const std::string& p_mode)
                  {
                      boost::shared_ptr<GPIOPin> pin(new GPIOPin(p_pinNumber, p_mode));
                      if (pin)
                      {
                          m_pinsInUse.insert(std::make_pair<std::string, boost::shared_ptr<GPIOPin> >(p_pinNumber, pin));
                          return true;
                      }
                      return false;
                  }
                  

                  这段代码一直被编译,但是当我添加 -std=c++0x 标志时,这段代码无法编译并显示以下消息:

                  This code has always compiled, but when I added the -std=c++0x flag this code fails to compile with the message:

                  [ 42%] Building CXX object gpioaccess/CMakeFiles/gpioaccess.dir/pinmanager/pinmanager.cpp.o
                  /home/pi/robot_2.0/trunk/gpioaccess/pinmanager/pinmanager.cpp: In member function 'bool gpioaccess::PinManager::insertPin(const string&, const string&)':
                  /home/pi/robot_2.0/trunk/gpioaccess/pinmanager/pinmanager.cpp:39:101: error: no matching function for call to 'make_pair(const string&, boost::shared_ptr<gpioaccess::GPIOPin>&)'
                  /home/pi/robot_2.0/trunk/gpioaccess/pinmanager/pinmanager.cpp:39:101: note: candidate is:
                  /usr/include/c++/4.6/bits/stl_pair.h:262:5: note: template<class _T1, class _T2> std::pair<typename std::__decay_and_strip<_T1>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
                  gpioaccess/CMakeFiles/gpioaccess.dir/build.make:77: recipe for target 'gpioaccess/CMakeFiles/gpioaccess.dir/pinmanager/pinmanager.cpp.o' failed
                  make[2]: *** [gpioaccess/CMakeFiles/gpioaccess.dir/pinmanager/pinmanager.cpp.o] Error 1
                  CMakeFiles/Makefile2:75: recipe for target 'gpioaccess/CMakeFiles/gpioaccess.dir/all' failed
                  make[1]: *** [gpioaccess/CMakeFiles/gpioaccess.dir/all] Error 2
                  Makefile:75: recipe for target 'all' failed
                  make: *** [all] Error 2
                  

                  经过一番挖掘,我发现之前编译的事实可能是一个错误;但是,我仍然不确定如何解决这个问题.有没有人在正确的方向上有任何要点?

                  After doing a little digging I found that the fact this compiled before was probably a bug; however, I am still unsure how to fix this. Does anyone have any points in the correct direction?

                  gcc --versiongcc (Debian 4.6.3-14+rpi1) 4.6.3

                  推荐答案

                  std::make_pair 在 c++03 和 c++11.

                  在c++11,它通过转发引用而不是通过值来接受参数.这意味着通过明确指定其类型模板参数,您最终会得到以下实例化:

                  In c++11, it accepts parameters by forwarding-references rather than by value. This means that by specifying its type template arguments explicitly you end up with the following instantiation:

                  std::make_pair<std::string , boost::shared_ptr<GPIOPin> >
                               (std::string&&, boost::shared_ptr<GPIOPin>&&);
                  

                  期望 rvalue 引用,它不能绑定到左值.

                  expecting rvalue references, which cannot bind to lvalues.

                  您不应明确指定 std::make_pair 的类型模板参数.相反,您应该让编译器自行推断它们:

                  You should not specify type template arguments of std::make_pair explicitly. Instead, you should let the compiler deduce them on its own:

                  std::make_pair(p_pinNumber, pin)
                  

                  此外,由于 c++11,当使用 std::map 时,你可以就地构造对:

                  Additionally, since c++11, when using std::map, you can construct the pair in-place:

                  m_pinsInUse.emplace(p_pinNumber, pin);
                  

                  在c++17 你可以利用类模板参数推导,并在不指定类模板参数的情况下构造std::pair:

                  In c++17 you can utilize class template argument deduction, and construct std::pair without specifying class template arguments:

                  std::pair(p_pinNumber, pin)
                  

                  这篇关于std::make_pair 与 C++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:文件/ifstream 上的双向迭代器 下一篇:我可以将 boost::make_shared 与私有构造函数一起使用吗?

                  相关文章

                  <legend id='mwhuW'><style id='mwhuW'><dir id='mwhuW'><q id='mwhuW'></q></dir></style></legend>
                    <bdo id='mwhuW'></bdo><ul id='mwhuW'></ul>
                • <small id='mwhuW'></small><noframes id='mwhuW'>

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