Error: "couldn't infer template argument '_Tp' " when passing in {} into

485 Views Asked by At
struct compar {
  bool operator()(const vector<int>& a,
                  const vector<int>& b) const {
    return a[1] < b[1];
  }
};

...

auto it = lower_bound(events.begin(), events.end(), {0, events[i][0]}, compar());

This code gives me an error with the {0, events[i][0]}:

/bits/stl_algo.h:2022:5: note: candidate template ignored: couldn't infer template argument '_Tp'
    lower_bound(_ForwardIterator __first, _ForwardIterator __last,
    ^

/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/algorithmfwd.h:353:5: note: candidate function template not viable: requires 3 arguments, but 4 were provided
    lower_bound(_FIter, _FIter, const _Tp&);
    ^
1 error generated.

But when I define it as a vector explicitly, it works as desired.

vector<int> point = {0, events[i][0]};
      auto it = lower_bound(events.begin(), events.end(), point, compar());

Can someone explain why?

1

There are 1 best solutions below

0
On BEST ANSWER

Braced-init-list has no type itself, it can't be used for deduction of template parameter. This is non-deduced context:

In the following cases, the types, templates, and non-type values that are used to compose P do not participate in template argument deduction, but instead use the template arguments that were either deduced elsewhere or explicitly specified. If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.

  1. The parameter P, whose A is a braced-init-list, but P is not std::initializer_list, a reference to one (possibly cv-qualified), or a reference to an array:

As you're showed, you have to specify the type explicitly, e.g.

auto it = lower_bound(events.begin(), events.end(), vector<int>{0, events[i][0]}, compar());
//                                                  ^^^^^^^^^^^

Or specify template arguments as

auto it = lower_bound<decltype(events.begin()), std::vector<int>>(events.begin(), events.end(), {0, events[i][0]}, compar());
//                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^