为了账号安全,请及时绑定邮箱和手机立即绑定

带有指定模板参数的C ++ 11 make_pair无法编译

带有指定模板参数的C ++ 11 make_pair无法编译

C++
UYOU 2019-08-27 10:45:56
带有指定模板参数的C ++ 11 make_pair无法编译我正在使用启用了-std = c ++ 11的g ++ 4.7(后面的一个快照)。我试图编译一些现有的代码库,一个失败的案例让我感到困惑。如果有人能解释发生了什么,我将不胜感激。这是代码:#include <utility>#include <iostream>#include <vector>#include <string>int main ( ){     std::string s = "abc";     // 1 ok     std::pair < std::string, int > a = std::make_pair ( s, 7 );     // 2 error on the next line     std::pair < std::string, int > b = std::make_pair < std::string, int > ( s, 7 );     // 3 ok     std::pair < std::string, int > d = std::pair < std::string, int > ( s, 7 );     return 0;}我知道make_pair 意味着用作(1)情况(如果我指定类型,那么我也可以使用(3)),但我不明白为什么它在这种情况下失败了。确切的错误是:test.cpp: In function ‘int main()’:     test.cpp:11:83: error: no matching function for call to ‘make_pair(std::string&, int)’     test.cpp:11:83: note: candidate is:     In file included from /gcc4.7/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include/c++/4.7.0/utility:72:0,                  from test.cpp:1:     /gcc4.7/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_pair.h:274:5:note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_T1>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)     /gcc4.7/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_pair.h:274:5:note:   template argument deduction/substitution failed:     test.cpp:11:83: note:   cannot convert ‘s’ (type ‘std::string {aka std::basic_string<char>}’) to type ‘std::basic_string<char>&&’同样,这里的问题只是“发生了什么?” 我知道我可以通过删除模板规范来解决问题,但我只是想知道这里的失败是什么。g ++ 4.4编译此代码没有任何问题。删除-std = c ++ 11也可以编译代码而没有任何问题。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 951 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信