eldruin eldruin - 18 days ago 5
C++ Question

C++11 bound method signature simplification

Given this code:

#include <iostream>
#include <functional>

template<typename T>
void f(T t1, T t2) { std::cout << t1 << " " << t2 << std::endl; }

template<typename...Ts>
void g(Ts... ts) { f(ts...); }

int main() {
auto f = std::bind(g<int, int>, 1, 2);
f();
return 0;
}


Is there any way to skip the template type specification for
g
in
std::bind(g<int, int>, 1, 2)
?

If not, is it at least possible to specify the type just once? i.e.
std::bind(g<int>, 1, 2)


Here is a live version of this.

Answer

I'm not sure if it's possible with bind, but you get same functionality with a lambda, and no need for explicit template params:

auto f = []{
    g(1, 2);
};