8

I'm trying to create a templated function with an optional argument and I'm having trouble understanding why the compile fails. This is my test (contrived) code:

#include <iostream>
#include <vector>

template <class UnaryPredicate>
int GetCountIf(std::vector<int> v, UnaryPredicate pred = [](auto) { return true; }) {
  int count=0;
  for (auto i: v) {
    if (pred(i)) {
      count++;
    }
  }
  return count;
}

int main() {
  auto v = std::vector<int>{0, 1, 2, 3, 4, 5};
  std::cout << "NumOddElements=" << GetCountIf(v, [](auto val) { return (val % 2 == 1); }) << '\n';
  // std::cout << "NumElements=" << GetCountIf(v) << '\n';
}

The code compiles only if I call GetCountIf() with both arguments. If I try to pass it only 1 argument, compilation fails with this error:

main.cpp:18:34: error: no matching function for call to 'GetCountIf'
std::cout << "NumElements=" << GetCountIf(v) << '\n'; ^~~~~~~~~~ main.cpp:5:5: note: candidate template ignored: couldn't infer template argument 'UnaryPredicate' int GetCountIf(std::vector v, UnaryPredicate pred = { return true; }) { ^ 1 error generated.

When the compiler comes across a call to GetCountIf with only 1 argument, why is it not able to deduce that the type of the optional lambda? If I explicitly specify the type of the predicate like this, it works:

template <typename T, class UnaryPredicate = std::function<bool(T)>>
int GetCountIf(std::vector<T> v, UnaryPredicate pred = [](T) { return true;}) {
  ...
}

Why does this work?

(I'm using C++14)

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
kshenoy
  • 1,726
  • 17
  • 31

2 Answers2

8

Note that the default value of function parameter won't be used for template argument deduction of template parameter; which leads to template argument deduction failure, the type of UnaryPredicate can't be deduced.

See non-deduced contexts.

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.

4) A template parameter used in the parameter type of a function parameter that has a default argument that is being used in the call for which argument deduction is being done:

template<typename T, typename F>
void f(const std::vector<T>& v, const F& comp = std::less<T>());
std::vector<std::string> v(3);
f(v); // P1 = const std::vector<T>&, A1 = std::vector<std::string> lvalue
      // P1/A1 deduced T = std::string
      // P2 = const F&, A2 = std::less<std::string> rvalue
      // P2 is non-deduced context for F (template parameter) used in the
      // parameter type (const F&) of the function parameter comp,
      // that has a default argument that is being used in the call f(v)

and

Type template parameter cannot be deduced from the type of a function default argument:

template<typename T> void f(T = 5, T = 7);

void g()
{
    f(1);     // OK: calls f<int>(1, 7)
    f();      // error: cannot deduce T
    f<int>(); // OK: calls f<int>(5, 7)
}

On the other hand, if you specify a default value std::function<bool(T)> for template parameter UnaryPredicate, then it'll be used as the type for UnaryPredicate if the argument for UnaryPredicate is not explicitly specified or not be deduced.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • 2
    A default parameter of `bool(T)` would do the trick and not need the type erasure of `std::function`. – user975989 Feb 21 '19 at 06:55
  • 1
    Thanks, type deduction never fails to make my head spin. @user975989 I like `bool(T)` better than using the `std::function` wrapper. Thanks for suggesting that. – kshenoy Feb 21 '19 at 15:20
0

Default argument does participate to deduction of the type, so the behavior you observe.

As alternative, you might create overloads:

template <class UnaryPredicate>
int GetCountIf(const std::vector<int>& v, UnaryPredicate pred) {
    int count = 0;
    for (auto i: v) {
        if (pred(i)) {
          count++;
        }
    }
    return count;
}

int GetCountIf(const std::vector<int>& v) {
    return GetCountIf(v, [](auto) { return true; }); // return v.size(); // :-)
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302