Noteworthy changes when porting to C++17

Ivan Čukić ivan.cukic at kde.org
Mon Jul 19 12:05:57 BST 2021


> > What I have seen is that std::mem_fun was used within KIO and has been
> > replaced by std::mem_fn. Not sure if that counts as "commonly used",
> > though.

> Imo, usages of either of these two should be rewritten to use lambdas
> instead.

-1

Member function pointers are more readable than lambdas in cases
where they can be used (std::ranges). Sadly, mem_fn is a syntactic noise
meant as an excuse why some things don't accept all invokables/callables
but only function objects. But, while it is uglier than just a pointer to a 
member function, I still find it more readable then lambdas. And it is 
optimized out by good compilers (tested on gcc 10) just as lambdas are.

std::ranges::all_of(vs, &some_t::check);

versus

std::all_of(..., std::mem_fn(&some_t::check));

versus

std::all_of(..., [] (const some_t& t) {
    return t.check();
});

Cheers,
Ivan

-- 
dr Ivan Čukić
ivan at cukic.co, https://cukic.co/
gpg key fingerprint: 8FE4 D32F 7061 EA9C 8232  07AE 01C6 CE2B FF04 1C12





More information about the Kde-frameworks-devel mailing list