#include <utility>
#include <vector>
using namespace std;
std::pair<std::size_t, std::size_t> func(const std::vector<int>& numbers, int target) {
for(std::size_t i =0; i < numbers.size(); i++)
{
for(std::size_t j = i; j < numbers.size(); j++)
{
if(numbers[i] + numbers[j] == target)
return std::make_pair<std::size_t, std::size_t>(i,j);
}
}
return std::make_pair<std::size_t, std::size_t>(0,0);
}
Error:
test.cpp: In function ‘std::pair<long unsigned int, long unsigned int> func(const std::vector<int>&, int)’:
test.cpp:12:62: error: no matching function for call to ‘make_pair<std::size_t, std::size_t>(std::size_t&, std::size_t&)’
return std::make_pair<std::size_t, std::size_t>(i,j);
^
In file included from /usr/include/c++/7/utility:70:0,
from test.cpp:1:
/usr/include/c++/7/bits/stl_pair.h:524:5: note: candidate: 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&&)
make_pair(_T1&& __x, _T2&& __y)
^~~~~~~~~
/usr/include/c++/7/bits/stl_pair.h:524:5: note: template argument deduction/substitution failed:
test.cpp:12:62: note: cannot convert ‘i’ (type ‘std::size_t {aka long unsigned int}’) to type ‘long unsigned int&&’
return std::make_pair<std::size_t, std::size_t>(i,j);
^
Why does it try to substitute with unsigned long int
when we have clearly mentioned the type in templated pair
and also the datatype of i
and j
?
When you specifying template argument as std::size_t
, the type of function parameter of std::make_pair
becomes std::size_t&&
, i.e. an rvalue-reference; i
and j
are lvalues and can't be bound to rvalue-reference.
Just letting std::make_pair
do the template argument deduction like std::make_pair(i,j)
(template parameter is std::size_t&
, function parameter is std::size_t&
) would be fine. std::make_pair
takes forwarding references, it's supposed to accept both lvalues and rvalues (for std::make_pair(0,0)
, template parameter is std::size_t
, function parameter is std::size_t&&
).