std::make_pair 与 C++ 11

2021-12-24 00:00:00 gcc raspberry-pi c++ c++11 boost c++17 c++03

您好,我有以下代码:

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)

相关文章