2017-10-14

Perfect: forwarding

So, in C++ there's this (perfect) forwarding thing, because there's a forwarding problem.

Interesting thoughts at Perfect forwarding and universal references in C++, and there's the usual StackOverflow, e.g., here (a duplicate, somebody said), and here.

Another interesting article, which contains other links to be dug, is Too perfect forwarding.

My idea, as usual, is that C++ is flawed since it was in its cradle, and modern C++ is built over its own flawed legacy: it's almost impossible to clean up that mess (it would become another language…). As you can imagine, I wasn't and I am not a big fan of C++, though recent language standards made it more interesting and usable. For sure, C++ is a huge and (over)complicated language, and it happens also to be powerful, whatever it means.

I've played a little bit with std::forward in this gist.

Output:

========================================================= lvalue call
action_a&/*L1*/
========================================================= rvalue call
action_a&&/*string("0")*/
============================================== lvalue (no forwarding)
begin&
    action_a&/*L1*/
end&;
================================================= lvalue (forwarding)
forward&
    action_a&&/*L1*/
drawrof&;
============================================== rvalue (no forwarding)
begin&&
    action_a&/*string("1")*/
end&&;
================================================= rvalue (forwarding)
forward&&
    action_a&&/*string("2")*/
drawrof&&;

You should have a compiler which can compile at least C++11. Otherwise you can test this online using sites like cpp.sh

No comments:

Post a Comment