eldruin eldruin - 1 year ago 135
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; }

void g(Ts... ts) { f(ts...); }

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

Is there any way to skip the template type specification for
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 Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download