Back to Basics: Forwarding References - How to Forward Parameters in Modern C++ - Mateusz Pusz 2023

Поделиться
HTML-код
  • Опубликовано: 25 авг 2024
  • cppcon.org/
    ---
    Back to Basics: Forwarding References - How to Forward Parameters in Modern C++ - Mateusz Pusz - CppCon 2023
    github.com/Cpp...
    This talk is a part of the Back To Basics track and provides the introduction to Forwarding References. During the talk, we will learn why those references are so important that they got a special name and how to use `std::forward` with them. We will put a lot of effort into understanding when we are dealing with them in the source code, and when it is not the case. We will also try to answer the question if they really deserve a "universal reference", as some people call them, and see how they may improve the overload set we write. Finally, we will learn about potential pitfalls they may introduce and how to handle them.
    ---
    Mateusz Pusz
    A software architect, principal engineer, and security champion with over 20 years of experience designing, writing, and maintaining C++ code for fun and living. A trainer with over 10 years of C++ teaching experience, consultant, conference speaker, and evangelist. His main areas of interest and expertise are Modern C++, code performance, low latency, safety, and maintainability.
    Mateusz worked at Intel for 13 years, and now he is a Principal Software Engineer and the head of the C++ Competency Center at EPAM Systems. He is also a founder of Train IT, which provides dedicated C++ trainings and consulting services to corporations worldwide.
    Mateusz is a contributor and an active voting member of the ISO C++ Committee (WG21), where, together with the best C++ experts in the world, he shapes the future of the C++ language.
    ---
    Videos Filmed & Edited by Bash Films: www.BashFilms.com
    RUclips Channel Managed by Digital Medium Ltd: events.digital...
    ---
    Registration for CppCon: cppcon.org/reg...
    #cppcon #cppprogramming #cpp

Комментарии • 12

  • @snbv5real
    @snbv5real 5 месяцев назад +25

    Great talk, but holy shit, decltype(auto) for *perfect returns*? All those gotchas with constructors? What an insane set of knowledge to have to remember to use this correctly. Really should have added &&& at the very least for universal/forwarding references... can't believe we need to sfinae or use concepts just to accept generic rvalue template parameters.

    • @user-nw8pp1cy8q
      @user-nw8pp1cy8q 5 месяцев назад +4

      I think, they should have used just different symbol.

    • @MateuszPusz
      @MateuszPusz 5 месяцев назад +4

      Thanks! I am glad you liked it. And yes, C++ is a hard language to master if you want to know every single detail...

    • @cyrilemeka6987
      @cyrilemeka6987 5 месяцев назад

      ​​@@MateuszPuszhey, awesome talk. C++ really is a complex nuanced language. You were right when you said the standards committee should have just added different syntax for prefect forwarding, - triple ampersands, you said😂.

    • @fahimp3
      @fahimp3 5 месяцев назад

      @@MateuszPusz Cool talk! Do you have any books you would recommend to learn all this stuff?
      At my work we mostly use Go and C++ is the legacy codebase, but I always wanted to get better at C++ 😀

    • @alurma
      @alurma 5 месяцев назад +1

      Great Rust and better C advertisement

  • @davidsicilia5316
    @davidsicilia5316 4 месяца назад +1

    always good talks from Mateusz

  • @sirnawaz
    @sirnawaz 5 месяцев назад +4

    3:50. `xvalue` does not necessarily have to be something on which `std::move` is applied. There is more to the story... `xvalue` is also those local lvalues which you return from a function (even if you do not apply std::move on them). So if you have local `std::string s` in a function and then you write `return s;` . In the return statement, `s` is an `xvalue` , means it's going to expire soon and so the compiler will automatically move it, without you having to type `return std::move(s)`. In fact, if you write `std::move(s)` on the return value, it's a bad idea, because it might discourage the compiler from doing move-elision optimization.

  • @felixbors7546
    @felixbors7546 4 месяца назад +1

    Until 23 I was becoming and second guessing the compiler, now I have to fight the compiler (make sure it doesn't infer shit behind my back). What happened to C++?

  • @enricomariadeangelis2130
    @enricomariadeangelis2130 3 месяца назад +1

    "prvalues are [...] things returned from the function"? A function can return a reference, hence an lvalue, which you can get the address of. If he said "values returned from the function", it would sound a bit more accurate (as opposed to "references returned from the function").
    Ok, I kept listening and noticed other things, which are already pointed out in other comments, and I find the explanations a bit handwavy.
    But a good presentation overall!

  • @SwitchTo1337
    @SwitchTo1337 5 месяцев назад

    Is there a mistake in the summary slide? He mentions that taking a parameter by value and move into the sink is a fine compromise for cheap to move values at 37:15. but the summary says use by value for expensive

    • @sebastianwilke626
      @sebastianwilke626 5 месяцев назад +1

      I wouldn't say mistake, but - as your comment suggests - maybe unfortunately formulated. 'Expensive scenarios' in the summary slide refers to the scaling problem (amount of code to write) he described prior and not the cost of taking by value and moving (for cheaply moved types). The number of overloads you need to write for what he calls 'the most efficient overload set', scales with 2^n, where 2 is talking by const lvalue reference and rvalue reference, and n is the number of values stored in the sink.