Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the replacement for std::function::argument_type?

Tags:

c++

function

According to cppreference.com all of the follwing three: argument_type, first_argument_type and second_argument_type are deprecated in C++17 and removed in C++20.

What is the standard library replacement for those member types? I mean I could write my own type traits, but I doubt that something gets removed without having a proper replacement in the standard library.

As an example:

template <typename F> 
void call_with_user_input(F f) {
    typename F::first_argument_type x;  // what to use instead ??
    std::cin >> x;
    f(x);
}
like image 974
463035818_is_not_a_number Avatar asked Sep 28 '18 13:09

463035818_is_not_a_number


People also ask

What does std:: mem_ fn do?

Function template std::mem_fn generates wrapper objects for pointers to members, which can store, copy, and invoke a pointer to member. Both references and pointers (including smart pointers) to an object can be used when invoking a std::mem_fn .

Why is std :: function slow?

If it is small, like 3-5 CPU instructions then yes std::function will make it slower, because std::function is not inlined into outer calling code. You should use only lambda and pass lambda as template parameter to other functions, lambdas are inlined into calling code.


1 Answers

You can get the type by introducing template parameters

template <typename Ret, typename Arg> 
void call_with_user_input(std::function<Ret(Arg)> f) {
    Arg x;
    std::cin >> x;
    f(x);
}

Gives you the argument type as a template parameter. As a bonus you also get the return type if you need it.

like image 132
NathanOliver Avatar answered Sep 25 '22 15:09

NathanOliver