The C++ Lambdas

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ต.ค. 2024

ความคิดเห็น • 17

  • @HomeofLawboy
    @HomeofLawboy ชั่วโมงที่ผ่านมา

    This is pretty high quality, hope this channel grows a lot

  • @bsdooby
    @bsdooby 5 ชั่วโมงที่ผ่านมา

    Awesome 🎉 as always; and yes to Concepts 👍

  • @zamf
    @zamf วันที่ผ่านมา +5

    Lambdas are my favourite feature of C++. They enable so much expressiveness and because of them some consider C++ to be a functional programming language (or at least a programming language that can be written in a functional style).
    You should also do a video on std::function, which combined with lambdas makes for some really powerful coding patterns.
    Also, in C++23 they introduced std::move_only_function, which is a more optimized version of std::function for captured objects that are not copyable.
    And in C++26 they're introducing std::function_ref, which is a light-weight version of std::function.
    As for lambdas, it should be noted that they can be templated as well.
    And one more important thing about lambdas is that they return their result by value by default, meaning that if you want a lambda that returns a reference you need to specify its return type as "auto&" or "decltype(auto)" or something similar to this.

    • @CodeForYourself
      @CodeForYourself  วันที่ผ่านมา +4

      @@zamf thanks! All good points! I’m still mostly targeting C++17 here just to make sure I have something finished before moving on to new standards. So no goodies that you described.
      Also, I’m not trying to completely copy cppreference here. The aim is to give people enough knowledge to be able to dig further. Hope this explains a bit better some choices I make in these videos.

    • @zamf
      @zamf วันที่ผ่านมา

      ​@@CodeForYourself I agree. I just think that std::function is a good candidate for a future video as a follow up to this one.

    • @CodeForYourself
      @CodeForYourself  วันที่ผ่านมา +2

      @@zamf definitely. That video I really want to record at some point but a proper deep dive into how it works requires type erasure and I’m not sure how long it will take me to distill it into a manageable explanation 😅

    • @Maximus98245
      @Maximus98245 วันที่ผ่านมา

      The biggest issue with std::function is that it doesn't respect the const-ness (other than a smaller issue of it might need to allocate and takes up 32 bytes as opposed to 8 for function pointer)
      struct Functor { int x = 0; void operator()() { ++x; } };
      const Functor func;
      //func(); // good, this will not compile
      const std::function stdfunc = Functor{};
      stdfunc(); // compiles, I can mutate const
      std::println("stdfunc.target()->x == {}", stdfunc.target()->x);

    • @zamf
      @zamf วันที่ผ่านมา

      @@Maximus98245 That is weird, I agree. But it's a similar behaviour to const pointers and even to const std::reference_wrapper. So not that unexpected.
      At least it modifies a local copy of the data, so nothing else in the program is modified.

  •  วันที่ผ่านมา +5

    I'd appreciate a lecture on concepts! Bare templates are a bit confusing and their error messages are confusing. I guess concepts help with understanding what the types are for in the template.

    • @CodeForYourself
      @CodeForYourself  วันที่ผ่านมา

      Yes, exactly. Let’s see if I get to record a video about this.

  • @Maximus98245
    @Maximus98245 วันที่ผ่านมา +2

    Great content! Yes to Concepts!! and multi-threading..and coroutines too..ok, I am pushing it I know...

    • @CodeForYourself
      @CodeForYourself  วันที่ผ่านมา

      @@Maximus98245 coroutines probably won’t happen. Everything else might 😉

    • @zamf
      @zamf วันที่ผ่านมา

      @@CodeForYourself Yeah, coroutines are for library developers. Not something that can be explained in a 10-20 minute youtube video.

  • @viktordiadkov8768
    @viktordiadkov8768 วันที่ผ่านมา +1

    Amazing! comment to promote

    • @CodeForYourself
      @CodeForYourself  วันที่ผ่านมา

      @@viktordiadkov8768 thanks! Highly appreciate it! 🙌

  • @segfault4568
    @segfault4568 วันที่ผ่านมา

    Great video, would be nice to cover function pointers and std::functions also the differences and use cases.

    • @CodeForYourself
      @CodeForYourself  วันที่ผ่านมา +1

      @@segfault4568 yes, absolutely! I hope to be able to get to it at some point. But as I mentioned before to properly talk about std::function I would kinda need to chat about type erasure and I’m not sure if I should or if I can do it in a simple enough way. Thinking about it anyway.