什麼是錯用下面的代碼:的std :: make_pair:不能轉換 '通道'(類型 '字符')鍵入 '字符&&'
g++ test.cxx
:
#include <ctime>
#include <vector>
#include <utility>
#include <algorithm>
#include <iostream>
int main()
{
std::vector< std::pair< char, unsigned > > vec;
for(unsigned i = 0; i < 100; ++i)
{
char ch = 0;
unsigned number = 0;
do {
ch = i;
number = i;
} while(std::find(vec.begin(), vec.end(), std::make_pair< char, unsigned >(ch, number)) != vec.end());
std::cout << ch << number << '\n';
vec.push_back(std::make_pair< char, unsigned >(ch, number));
}
}
它有很好的編譯
但失敗:
$ g++ -std=c++11 test.cxx /tmp
test.cxx: In function 'int main()':
test.cxx:21:98: error: no matching function for call to 'make_pair(char&, unsigned int&)'
test.cxx:21:98: note: candidate is:
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.7/vector:61,
from test.cxx:3:
/usr/include/c++/4.7/bits/stl_pair.h:268:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
/usr/include/c++/4.7/bits/stl_pair.h:268:5: note: template argument deduction/substitution failed:
test.cxx:21:98: note: cannot convert 'ch' (type 'char') to type 'char&&'
test.cxx:25:69: error: no matching function for call to 'make_pair(char&, unsigned int&)'
test.cxx:25:69: note: candidate is:
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.7/vector:61,
from test.cxx:3:
/usr/include/c++/4.7/bits/stl_pair.h:268:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
/usr/include/c++/4.7/bits/stl_pair.h:268:5: note: template argument deduction/substitution failed:
test.cxx:25:69: note: cannot convert 'ch' (type 'char') to type 'char&&'
爲什麼地球上,你會通過模板參數首先放在'std :: make_pair'中..? – ildjarn 2013-02-26 23:37:15