79516670

Date: 2025-03-18 07:46:52
Score: 0.5
Natty:
Report link

As explained by @Anton Savin, the two lambdas have different types. Similarily to this s/o question, if you'd still really want to use only one template parameter type, you can do so by resorting to std::function<> which was introduced in C++11. As I explained in another answer, it ensures your two function arguments have the same types:

#include <iostream>
#include <functional>

template <class A, class OP>
auto WhichOp2(A argument, OP firstOp, OP secondOp)->decltype(firstOp(argument)) {
    return firstOp(argument) + secondOp(argument);
}

int main()
{
    int d = WhichOp2(
        2, 
        std::function<int(int)>([](int i){return i * 2; }), 
        std::function<int(int)>([](int i){return i * 3; })
    );

    std::cout << "result : " << d << std::endl; 
}

DEMO here

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Anton
  • Low reputation (0.5):
Posted by: grybouilli