Paul Dreik: What is this std::forward thing?

Ғылым және технология

Recorded at Swedencpp 20171026 at King Stockholm, connected to London via video link. This was probably the world's first distributed C++ meetup.
See the whole evening, including the talks from London, in one video:
• C++ London && SwedenCpp

Пікірлер: 20

  • @AndrewHelgeCox
    @AndrewHelgeCox6 жыл бұрын

    I think it would be useful to talk about why the naive thing that you want to do (call f() directly from the wrapper) doesn’t work.

  • @Jack-dx7qb
    @Jack-dx7qb14 күн бұрын

    Here's why f(S&) is called nonetheless: In C++, when a temporary object is passed to a function, the compiler tries to find the best match among overloaded functions. It prioritizes functions taking rvalue references (f(S&&)) for efficiency reasons. However, in this specific case, there's a catch. The function f(S&) is also a viable candidate because a reference can bind to both values (variables) and rvalues (temporary objects). Since there's no explicit conversion happening within the wrap function (e.g., casting the temporary object to an lvalue), the compiler might choose the seemingly simpler option - f(S&).

  • @nixiz1
    @nixiz16 жыл бұрын

    Why don't u just call both of "wrap(s)" and "wrap(S{})" methods instead of comment out one of other every time

  • @justinjames3028

    @justinjames3028

    Жыл бұрын

    If he were testing for himself he might. But the audience might get confused by seeing both on the output. So, to keep it as simple as possible he only runs one at a time. And, honestly, why would you care. It has nothing to do with the topic at hand.

  • @rahul-patil
    @rahul-patil2 жыл бұрын

    template void wrap(T&& t){ f(t); } S s; wrap(s); Why is this not giving compiler error for rvalue value expected?

  • @digimikeh

    @digimikeh

    2 жыл бұрын

    have you overloaded f() ?

  • @zoso25

    @zoso25

    2 жыл бұрын

    T&& t is not an rvalue reference but a forwardding reference. It's really unfortunate that both forwarding and rvalue references have the same syntax.

  • @ilkeraktug4960

    @ilkeraktug4960

    Жыл бұрын

    template void wrap(T&& t) // This is forwarding void wrap(T&& t) //This is rvalue reference The difference is template.

  • @nocapodistrias4365

    @nocapodistrias4365

    Жыл бұрын

    @@zoso25 very useful comment

  • @picardjean9467

    @picardjean9467

    4 ай бұрын

    @@ilkeraktug4960 I am newbie to modern c++, but I think this is too crazy🤯

  • @LowellBoggs
    @LowellBoggs7 ай бұрын

    I am confused as to why it is considered advisable to let people pass a writable reference to an unnamed variable -- that is what is happening when you forward a reference of any non-const reference of any kind (forwarding or simple reference, I mean) to a function that might write on it. The changes made to it would be unavailable to the calling function since the object has no name in its context with which to refer to the data so changed. While I thought this was a good tutorial, i would have appreciated an actual use in the context of a real program that benefits from this feature.

  • @kenny3796
    @kenny37969 ай бұрын

    what does S( ) mean in f(S()), I don't remember a syntax like this for a C++ struct , what does it return? Thanks

  • @LowellBoggs

    @LowellBoggs

    7 ай бұрын

    S() means to construct an object of type S with no parameters but do not give it the name of a variable, just use it in the current expression, then destruct it at a time convenient to the compiler.

  • @ardeshiraminian9841
    @ardeshiraminian98415 жыл бұрын

    I really like these short and concise videos.

  • @digimikeh
    @digimikeh2 жыл бұрын

    i meet universal reference thanks to Scott Meyer

  • @eshgholah
    @eshgholah6 жыл бұрын

    I keep wondering what an OBYEKT is!!??

  • @mateusz-czajkowski

    @mateusz-czajkowski

    4 жыл бұрын

    obdżekt

  • @marcpawl
    @marcpawl5 жыл бұрын

    I love the group logo.

  • @raghavendranakod8360
    @raghavendranakod83604 жыл бұрын

    Simple and Best way to explain it thanks a lot

Келесі