With what can I replace std::unary_function?

unary_function has been removed in C++ 17. What can I replace it with?
Just see JLBorges explanation. (I would delete this if I could.)
Last edited on
The functionality of std::unary_function is subsumed by the polymorphic call wrapper std::function,
which supersedes it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <functional>
#include <cstring>

template < typename R, typename A > using my_unary_function = std::function< R(A) > ;

int main()
{
    my_unary_function< std::size_t, const char* > fn = &std::strlen ;
    fn = [] ( const char* cstr ) { return cstr ? std::strlen(cstr) : 0 ; } ;

    // note: members result_type and argument_type are deprecated (and removed in C++20)
    static_assert( std::is_same_v< std::size_t, decltype(fn)::result_type > ) ;
    static_assert( std::is_same_v< const char*, decltype(fn)::argument_type > ) ;
}

http://coliru.stacked-crooked.com/a/76ee4117ab98d889
Thanks JLBorges, as usual!!

Topic archived. No new replies allowed.