0

The following does not work:

#include <iostream>
#include <functional>

template<typename T>
auto or_combinator(T first, T second) {
    return [=](int x) { first(x) || second(x); };
}

int main()
{    
    auto is_zero = [](int x) { return (x == 0); };
    auto is_one  = [](int x) { return (x == 1); };
    auto combined = or_combinator(is_zero, is_one);
    
    std::cout << combined(2);
}

The error message is

template argument deduction/substitution failed:
deduced conflicting types for parameter ‘T’ (‘main()::<lambda(int)>’ and ‘main()::<lambda(int)>’)

For detailed error message and various other things I have tried, see coliru link. The code above seems to express what I want to do quite clearly (at least to me, heh). So why does it not work? Are the types of is_zero and is_one not the same? What is the correct way to accomplish what I am trying to do?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
dqbydt
  • 73
  • 9
  • To get the same type for `is_zero` and `is_one`, you can potentially get a function pointer from them, or wrap them with `std::function` – Ranoiaetep Nov 26 '22 at 00:38

0 Answers0