'C++ method that returns a type instead of an object

Having the following case

template <typename A, typename B>
void f(A a, std::function<void(B)> f2)
{
  B b = f1(a);

  f2(b);
}

A, B are structures, the association between A and B is 1 to 1 in my logic.

How can I create a function that receives type A as input, and results in type B as output so I can write instead:

template <typename A>
void f(A a, std::function<void(TypeFunction(A))> f2)
{
  auto b = f1(a);

  f2(b);
}


Solution 1:[1]

If there are no other overloads of f, then you don't need to specify B at all.

template<typename A, typename FB>
void f(A a, FB f2) {
    auto b = f1(a);
    f2(b);
}

If there are other overloads, such that you require a std::function argument, then you can use decltype

template<typename A>
void f(A a, std::function<void(decltype(f1(a)))> f2) {
    auto b = f1(a);
    f2(b);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Caleth