Binding Functions with Various Signatures

If this question is asked, I apologize.

I thought you cannot bind functions with a different signature, but look at this:

void TakesChar(char parameter)
{
    std::cout << parameter << std::endl;
}

using CallSig = void(float);
using CallBack = std::function<CallSig>;

int main()
{
    CallBack callback = std::bind(&TakesChar, std::placeholders::_1);
    callback(1.1f);
    callback(2.2f);

    return 0;
}

It compiles and runs. You can try different types and parameter numbers. You can, for example, change TakesCharso that it does not accept any parameters, and it will still compile.

Why is this? Is there any justification for this? And can I ensure that the signatures match exactly?

Thank.

+4
source share
1 answer

Actually, there are two questions:

  • Why is conversion allowed?
  • , bind , ?

- bind: TakesChar(1.1f), std::bind(&TakesChar, _1) std::function<void(float)>? .

, , fooobar.com/questions/57947/.... , , bind , operator() .

+1

Source: https://habr.com/ru/post/1538744/


All Articles