7
#include <iostream>
#include <functional>

using callback = std::function<void(int, void*)>;

void AddCallback(callback cb) {}

void foo(int i) {}

int main() {
  auto f = std::bind(&foo, std::placeholders::_1);
  AddCallback(f);
}

I tried the code with g++ 9.3.0 and clang++ 10.0.0, they both compiled ends no errors.

Is the type of bind result and callback the same type? One is std::function<void(int, void*)>, the other is something equal to std::function<void(int)>? Why can I call AddCallback() with different types?

Evg
  • 25,259
  • 5
  • 41
  • 83
Xingx1
  • 127
  • 7

2 Answers2

3

It seems you can pass more arguments to the result of bind than necessary, and they will be silently ignored.

If some of the arguments that are supplied in the call to [the result of bind] are not matched by any placeholders ..., the unused arguments are evaluated and discarded.

cppreference

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 1
    Is the type of bind result and callback the same type? why can I call AddCallback() with different types? One is std::function, the other is something equal to std::function? – Xingx1 Jul 03 '20 at 09:09
  • @Xingx1 Because `std::function` can be constructed from any callable that you can call with the specified arguments. If you can call the result of `std::bind` using the arguments of the specified types, it can be converted to a `std::function`. – IS4 Jul 03 '20 at 09:19
  • 2
    The result of `std::bind` is a type with a templated `operator()`. It **isn't** an instantiation of `std::function`, rather `std::function` has a converting constructor that accepts the binder type – Caleth Jul 03 '20 at 09:20
0

it's not your AddCallback() whose accepting differnt type but it's type deduction whose creating confusing;

for example try to use

std::function<void(int)> f = std::bind(&foo, std::placeholders::_1);

or Addcallback function we will give you error

Error (active) E0312 no suitable user-defined conversion from "std::function<void (int)>" to "callback" exists

Nilesh Solanki
  • 336
  • 4
  • 19
  • 1
    The problem is that result of `std::bind` call is not `std::function`, it's an unspecified type that can be converted to `std::function`. If you changed type of `f` to `std::function`, it would compile perfectly. – Yksisarvinen Jul 03 '20 at 09:33
  • 1
    That's true. That type of std::bind is unspecified but his question is "Why can I call AddCallback() with different types?" – Nilesh Solanki Jul 03 '20 at 09:38