Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using std::apply on std::bind

Tags:

c++

std

c++17

The following fails to compile with gcc 8.2.0 [stacktrace]

#include <iostream>
#include <tuple>
#include <functional>

void print(int a, int b, int c) {
  std::cout << a << ", " << b << ", " << c << std::endl;
}

int main() {
  auto b = std::bind(print, 1);
  auto t = std::make_tuple(2, 3);

  std::apply(b, t);
}

I personally fail to see why, it would seem to be common sense for it to work (in a proper functional language at least).

Could somebody explain the reason it doesn't work?

like image 291
TonCherAmi Avatar asked Sep 25 '26 09:09

TonCherAmi


1 Answers

With std::bind you need to also provide placeholders for the unbound arguments.

auto b = std::bind(print, 1, std::placeholders::_1, std::placeholders::_2);
like image 119
David G Avatar answered Sep 28 '26 05:09

David G