的std :: make_pair与C ++ 11 [英] std::make_pair with c++ 11

查看:196
本文介绍了的std :: make_pair与C ++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有以下code:

Hello I have the following code:

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;
}

这code一直编译,但是当我添加了 -std =的C ++ 0x 将此code失败,该消息编译:

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 --version 海湾合作委员会(Debian的4.6.3-14 + rpi1)4.6.3

推荐答案

的std :: make_pair <​​/ code> 定义,C ++ 03和C ++ 11之间变化。在C ++ 11它需要的参数的通过转发引用的而不是通过值的。这意味着,通过明确指定其类型模板参数你结束了以下实例:

The std::make_pair definition has changed between C++03 and C++11. In C++11 it takes arguments 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>&&);

期待的右值的参考,不能结合您在通过左值。

expecting rvalue references, which cannot bind to lvalues that you pass in.

您不应该传递类型模板参数的std :: make_pair <​​/ code>明确,并让编译器推断出他们自身的:

You should not pass type template arguments to std::make_pair explicitly, and let the compiler deduce them on its own:

std::make_pair(p_pinNumber, pin)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆