C++ Weekly With Jason Turner
C++ Weekly With Jason Turner
  • 500
  • 8 002 696
C++ Weekly - Ep 428 - C++23's Coroutine Support: std::generator
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
Upcoming Workshop: Understanding Object Lifetime, C++ On Sea, July 2, 2024
► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html
Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c
CLion is a cross-platform JetBrains IDE for C and C++ with:
- A smart C and C++ editor to navigate and maintain your code base productively.
- Code analysis with quick-fixes to identify and fix bugs and style inconsistencies.
- An integrated debugger - along with other essential tools from the ecosystem - available
straight out of the box.
- And much more!
jb.gg/clion_ide code: CppWeeklyCLion
Episode details: github.com/lefticus/cpp_weekly/issues/377
T-SHIRTS AVAILABLE!
► The best C++ T-Shirts anywhere! my-store-d16a2f.creator-spring.com/
WANT MORE JASON?
► My Training Classes: emptycrate.com/training.html
► Follow me on twitter: lefticus
SUPPORT THE CHANNEL
► Patreon: www.patreon.com/lefticus
► Github Sponsors: github.com/sponsors/lefticus
► Paypal Donation: www.paypal.com/donate/?hosted_button_id=PQ4A2V6ZZFQEU
GET INVOLVED
► Video Idea List: github.com/lefticus/cpp_weekly/issues
JASON'S BOOKS
► C++23 Best Practices
Leanpub Ebook: leanpub.com/cpp23_best_practices/
► C++ Best Practices
Amazon Paperback: amzn.to/3wpAU3Z
Leanpub Ebook: leanpub.com/cppbestpractices
JASON'S PUZZLE BOOKS
► Object Lifetime Puzzlers Book 1
Amazon Paperback: amzn.to/3g6Ervj
Leanpub Ebook: leanpub.com/objectlifetimepuzzlers_book1
► Object Lifetime Puzzlers Book 2
Amazon Paperback: amzn.to/3whdUDU
Leanpub Ebook: leanpub.com/objectlifetimepuzzlers_book2
► Object Lifetime Puzzlers Book 3
Leanpub Ebook: leanpub.com/objectlifetimepuzzlers_book3
► Copy and Reference Puzzlers Book 1
Amazon Paperback: amzn.to/3g7ZVb9
Leanpub Ebook: leanpub.com/copyandreferencepuzzlers_book1
► Copy and Reference Puzzlers Book 2
Amazon Paperback: amzn.to/3X1LOIx
Leanpub Ebook: leanpub.com/copyandreferencepuzzlers_book2
► Copy and Reference Puzzlers Book 3
Leanpub Ebook: leanpub.com/copyandreferencepuzzlers_book3
► OpCode Puzzlers Book 1
Amazon Paperback: amzn.to/3KCNJg6
Leanpub Ebook: leanpub.com/opcodepuzzlers_book1
RECOMMENDED BOOKS
► Bjarne Stroustrup's A Tour of C++ (now with C++20/23!): amzn.to/3X4Wypr
AWESOME PROJECTS
► The C++ Starter Project - Gets you started with Best Practices Quickly - github.com/cpp-best-practices/cpp_starter_project
► C++ Best Practices Forkable Coding Standards - github.com/cpp-best-practices/cppbestpractices
O'Reilly VIDEOS
► Inheritance and Polymorphism in C++ - www.oreilly.com/library/view/inheritance-and-polymorphism/9781491961933/
► Learning C++ Best Practices - www.oreilly.com/library/view/learning-c-best/9781491954898/
Переглядів: 6 131

Відео

C++ Weekly - Ep 427 - Simple Generators Without Coroutines
Переглядів 9 тис.День тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c Episode details: github.com/lefticus/cpp_weekly/issues/304 T-SHIRTS...
C++ Weekly - Ep 426 - Lambdas As State Machines
Переглядів 13 тис.14 днів тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c CLion is a cross-platform JetBrains IDE for C and C with: - A smart...
C++ Weekly - Ep 425 - Using string_view, span, and Pointers Safely!
Переглядів 11 тис.21 день тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c This episode is sponsored by think-cell. think-cell.com/cppweekly E...
C++ Weekly - Ep 424 - .reset vs →reset()
Переглядів 10 тис.Місяць тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c CLion is a cross-platform JetBrains IDE for C and C with: - A smart...
C++ Weekly - Ep 423 - Complete Guide to Attributes Through C++23
Переглядів 9 тис.Місяць тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c This episode is sponsored by think-cell. think-cell.com/cppweekly E...
C++ Weekly - Ep 422 - Moving from C++20 to C++23
Переглядів 8 тис.Місяць тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c Episode details: github.com/lefticus/cpp_weekly/issues/359 T-SHIRTS...
C++ Weekly - Ep 421 - You're Using optional, variant, pair, tuple, any, and expected Wrong!
Переглядів 24 тис.Місяць тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c This episode is sponsored by think-cell. think-cell.com/cppweekly E...
C++ Weekly - Ep 420 - Moving From C++17 to C++20 (More constexpr!)
Переглядів 7 тис.Місяць тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c Episode details: github.com/lefticus/cpp_weekly/issues/359 T-SHIRTS...
C++ Weekly - Ep 419 - The Important Parts of C++23
Переглядів 12 тис.2 місяці тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c This episode is sponsored by think-cell. think-cell.com/cppweekly E...
CS101++ - What Are Open Source and GitHub?
Переглядів 1,5 тис.2 місяці тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c github.com/lefticus/cpp_weekly/discussions/categories/cs101 for ope...
CS101++ - What is`goto`?
Переглядів 1,6 тис.2 місяці тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c github.com/lefticus/cpp_weekly/discussions/categories/cs101 for ope...
C++ Weekly - Ep 418 - Moving From C++14 to C++17
Переглядів 7 тис.2 місяці тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c Create anything, find anything, fix anything, and navigate to anyth...
CS101++ - What is a `for` Loop?
Переглядів 1,2 тис.2 місяці тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c github.com/lefticus/cpp_weekly/discussions/categories/cs101 for ope...
CS101++ - What is a `do` Loop?
Переглядів 1,1 тис.2 місяці тому
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c github.com/lefticus/cpp_weekly/discussions/categories/cs101 for ope...
C++ Weekly - Ep 417 - Turbocharge Your Build With Mold?
Переглядів 6 тис.2 місяці тому
C Weekly - Ep 417 - Turbocharge Your Build With Mold?
CS101++ - What Are `if`/`else` Statements?
Переглядів 1,3 тис.2 місяці тому
CS101 - What Are `if`/`else` Statements?
CS101++ - What is a `while` Loop?
Переглядів 1,4 тис.2 місяці тому
CS101 - What is a `while` Loop?
C++ Weekly - Ep 416 - Moving From C++11 to C++14
Переглядів 6 тис.2 місяці тому
C Weekly - Ep 416 - Moving From C 11 to C 14
CS101++ - What Are Truth Tables?
Переглядів 1,7 тис.3 місяці тому
CS101 - What Are Truth Tables?
CS101++ - What Are Computability and the Halting Problem?
Переглядів 2,1 тис.3 місяці тому
CS101 - What Are Computability and the Halting Problem?
C++ Weekly - Ep 415 - Moving From C++98 to C++11
Переглядів 8 тис.3 місяці тому
C Weekly - Ep 415 - Moving From C 98 to C 11
CS101++ - Exploring Circuit Simulation Tools: Online & Offline
Переглядів 2,4 тис.3 місяці тому
CS101 - Exploring Circuit Simulation Tools: Online & Offline
CS101++ - What Are Logic Gates?
Переглядів 1,7 тис.3 місяці тому
CS101 - What Are Logic Gates?
C++ Weekly - Ep 414 - C++26's Placeholder Variables With No Name
Переглядів 11 тис.3 місяці тому
C Weekly - Ep 414 - C 26's Placeholder Variables With No Name
CS101++ - What Are Nibbles, Bits, Bytes and Words
Переглядів 2,3 тис.3 місяці тому
CS101 - What Are Nibbles, Bits, Bytes and Words
CS101++ - What Are Binary, Hexadecimal, Octal and Decimal?
Переглядів 1,5 тис.3 місяці тому
CS101 - What Are Binary, Hexadecimal, Octal and Decimal?
C++ Weekly - Ep 413 - (2x Faster!) What are Unity Builds (And How They Help)
Переглядів 7 тис.3 місяці тому
C Weekly - Ep 413 - (2x Faster!) What are Unity Builds (And How They Help)
CS101++ - What is an Operating System?
Переглядів 2,6 тис.3 місяці тому
CS101 - What is an Operating System?
CS101++ - How To Program Online with Compiler Explorer
Переглядів 3,1 тис.3 місяці тому
CS101 - How To Program Online with Compiler Explorer

КОМЕНТАРІ

  • @LesleyLai
    @LesleyLai День тому

    I know ranges are cool and offer many more functionalities (constexpr, potential random access, and so on). But it is such *much* easier to actually author generators than either traditional iterators or ranges.

  • @MaitreBart
    @MaitreBart День тому

    What is the _s: prefixing a string literal at 1min25sec, as the argument of puts(), at line 4? Is it c++?

  • @garrettkajmowicz
    @garrettkajmowicz День тому

    How do you avoid pointer arithmetic when attempting to parse network packets?

  • @luizkruger7425
    @luizkruger7425 2 дні тому

    This one I feel like could have gone more in depth.

  • @damnstupidoldidiot8776
    @damnstupidoldidiot8776 2 дні тому

    I have implemented function and it is very interesting.

  • @StefanDragan
    @StefanDragan 2 дні тому

    just use a struct or tuple or pair for the min/max

  • @shaartmcl
    @shaartmcl 2 дні тому

    Hey man, I was really enjoying this series and am just wondering if it will be continuing or not? If not, all good - just curious!

  • @user-ph6ct7ug8z
    @user-ph6ct7ug8z 3 дні тому

    std::generator allocates. The lambda function in the previous video returns a very weird type, since it is auto. Is there a way to have the best of both: a generator class/template, to which I tell to use a certain amount of memory to work with on the stack. kind of like the new flatmaps with arrays. e.g. std::generator<int&, int, std::array<char,512>> ?

  • @VickyGYT
    @VickyGYT 3 дні тому

    instead of using std::move you should just move away from c++

  • @VickyGYT
    @VickyGYT 3 дні тому

    The mess that C++ is or has become...

  • @garrettkajmowicz
    @garrettkajmowicz 3 дні тому

    If you didn't have the operations being performed in the various constructors, would the compilers be able to optimize those operations away?

  • @colinkennedy
    @colinkennedy 4 дні тому

    Or you could write a for loop and reduce the generated assembly from 338 lines of unreadable asm to 30 lines of pretty readable asm. Probably faster too, though quick-bench doesn't support GCC 14.1+ so I wasn't able to verify. 1. Copy to godbolt 2. Flip #if 1 to #if 0 3. See the assembly reduce by 90% ``` #if 1 #include <generator> #include <iostream> #include <utility> // std::exchange #include <ranges> std::generator<int> fib() { int i = 0; int j = 1; while (true) { co_yield i = std::exchange(j, i + j); } } int main() { for (const auto i: fib() | std::views::take(20)) { std::cout << i << ' '; } } #else #include <iostream> #include <utility> // std::exchange int main() { int i = 0; int j = 1; for (int index = 0; index < 20; ++index) { i = std::exchange(j, i + j); std::cout << i << ' '; } } #endif ```

  • @mrlumps8711
    @mrlumps8711 4 дні тому

    The difference in assembly output is quite large. I imagine the optimization maniacs have not had their fun yet with std generator

  • @sjswitzer1
    @sjswitzer1 4 дні тому

    Very nice to see generators landing but I’m not convinced that std::exchange is contributing to the transparency or readability of this code

    • @__Brandon__
      @__Brandon__ 4 дні тому

      He did say tersest way possible

    • @cppweekly
      @cppweekly 4 дні тому

      That is what I said. Also, exchange is super helpful when you need it, so learning how to read it is super helpful.

    • @wojciechrazik
      @wojciechrazik 3 дні тому

      I think that our brains get use to unusual syntax. When you learn C++ - lambdas look terrible. After a while you get use to it. The more you see std::exchange, it will be more natural.

  • @bryce.ferenczi
    @bryce.ferenczi 4 дні тому

    Going to have to get out of the habit of reaching for std::cout and use std::println instead. Its also interesting how much code hello world with std::println produces with -O(2/3), maybe an episode on why that is the case would be interesting. At compile time it should be known I'm printing a basic string and not using any formatting and remove all the formatting boilerplate etc.

    • @cppweekly
      @cppweekly 4 дні тому

      This is why I'm tending to not use it in C++ Weekly. It's unfortunately slow to compile and obfuscates the binary. I hope they make it better soon

  • @von_nobody
    @von_nobody 4 дні тому

    I would make some observation, code like `for (auto i : foo() | bar())` was tricky in older C++ as value returned from `foo()` could be destroyed as lifetime was bound only to `foo() | bar()` expression, but recent C++ fix it and make that temps in range `for` live as long this `for` is in scope.

    • @testtest-qm7cj
      @testtest-qm7cj 4 дні тому

      Are you referring to C++23 'Extending the lifetime of temporaries in range-based for loop initializer' ?

    • @von_nobody
      @von_nobody 3 дні тому

      @@testtest-qm7cj Yes, this, small change but how much it improve life

  • @Nobody1707
    @Nobody1707 5 днів тому

    I'm going to go out on a limb here and say that the lambda produces much, much better code than the generator. Godbolt short link: 5G3j54x8r

  • @alskidan
    @alskidan 5 днів тому

    The infinite loop is UB, isn’t it?

    • @Nobody1707
      @Nobody1707 4 дні тому

      I think not, because co_yield is a side effect. Also, I think C++26 makes while(true) { } defined anyway.

    • @arthapz
      @arthapz 4 дні тому

      @@Nobody1707 yes but not while(true) { int i = 0; } afaik

    • @Nobody1707
      @Nobody1707 4 дні тому

      @@arthapz No, C++26 I believe is aligning the rules to be closer to C's. If the loop condition is the literal true then the infinite loop is defined behavior. Other forms of infinite loops will still not be.

  • @Nobody1707
    @Nobody1707 5 днів тому

    -Fanalyzer does not like std::generator one bit. Also, here's a version of fib() with no UB that stops after the addition overflows: constexpr int wrapping_add(int a, int b) noexcept { return static_cast<unsigned>(a) + static_cast<unsigned>(b); } std::generator<int> fib() { int i = 0; int j = 1; while (true) { co_yield (i = std::exchange(j, wrapping_add(i, j))); if (j < i) { break; } } }

    • @Nobody1707
      @Nobody1707 5 днів тому

      And if you add a co_yield 0; before the loop it'll yield the classical version of the sequence.

    • @Nobody1707
      @Nobody1707 5 днів тому

      Actually, I made this too complicated, just yield I before you compute the next number in the sequence: std::generator<int> fib() { int i = 0; int j = 1; while (i >= 0) { co_yield i; i = std::exchange(j, wrapping_add(i, j)); } }

    • @driedurchin
      @driedurchin 4 дні тому

      Don’t you need a return outside the loop if you’re gonna break?

    • @Nobody1707
      @Nobody1707 4 дні тому

      @@driedurchin Nope. The generator stops yielding values when it reaches the end of the function. I think it returns some sort of stop token when you do that, so that loops over the generator know that you're done.

    • @driedurchin
      @driedurchin 4 дні тому

      @@Nobody1707 So it is different from a regular function in that control flow analysis doesn't care that you reach the end of a non-void function?

  • @darkmagic543
    @darkmagic543 5 днів тому

    Awesome, I hope other compilers will implement it soon as well so that people can start adapting it

  • @toast_on_toast1270
    @toast_on_toast1270 5 днів тому

    I'm a C++ beginner/intermediate (~3 years experience, 2 of which on the job (take from that what you will)), and it's my only language so far. I gotta say, it seems like the perfect time to be getting deeper into it: in the back of my mind, I've had "I must sometime learn about coroutines" - now I have the fancy new features to do it! I am starting to take learning this language more seriously and I feel like your videos are gonna help me learn fast.

  • @alexandrebustico9691
    @alexandrebustico9691 5 днів тому

    also compile with just released gcc 14.1

  • @literallynull
    @literallynull 5 днів тому

    What are the good use cases of coroutines? Where can I learn more about them? Maybe .pdf book or something

    • @TsvetanDimitrov1976
      @TsvetanDimitrov1976 5 днів тому

      I think Donald Knuth's The Art of Computer Programming is probably the best known motivation for the need for coroutines, although there surely are more modern books

    • @truesoundwave
      @truesoundwave 4 дні тому

      Iterative Tasking. Sometimes, you don't want to do all of your work up front. You want to do it in chunks. Writing code to support something like this is an absolute pain once it gets complicated.

  • @ohwow2074
    @ohwow2074 5 днів тому

    Imagine this combined with the upcoming networking library! I don't even know what exactly is possible.

    • @KulaGGin
      @KulaGGin 4 дні тому

      I never liked this approach. The default procedural approach working together with OOP is much easier to understand than this functional programming approach. I've seen even people who are proponents of functional programming admit that the default procedural + OOP is much easier to understand than this.

  • @mjKlaim
    @mjKlaim 5 днів тому

    Yay for coroutines! I've exploited to death `std::generator` in ways that works surprisingly through the years, it's very interesting what you can do with that as soon as you understand how to manipulate manually the iterator from `begin()`

  • @bitdevice
    @bitdevice 5 днів тому

    I always avoid static in function scope and it is also forbidden at my work because it introduces a mutex lock. Is there a guarantee that 'static constexpr' will never initialize at runtime and hence no mutex? Otherwise it's quite useless IMO. Best would be if there was a way to make static in function scope initialize at the same time as global scope so there was no need for it.

    • @cppweekly
      @cppweekly 2 дні тому

      Any static that is initialized with a constant expression (regardless of if the static itself is constexpr or even const) is "static initialized" which means it strongly happens before any user code is executed. So yes, this is required by the standard, hence why I recommend it. The latest version of my C++ Best Practices book actually digs into this some more too. leanpub.com/cpp23_best_practices

  • @broken_abi6973
    @broken_abi6973 5 днів тому

    It bothers me that coroutine-based generators couldn't be made malloc-free when this is possible...

  • @tim37021
    @tim37021 6 днів тому

    I know this behavior long time ago. So i always dare to return stack created object, since it is still fast enough. Handling rvalue reference sometimes hurt productivity.

    • @cppweekly
      @cppweekly 2 дні тому

      Returning a local stack created object is literally the best possible option. ua-cam.com/video/9mWWNYRHAIQ/v-deo.html

  • @reneb86
    @reneb86 6 днів тому

    Oh man. I like Jason's talks on CppCon. So was eager to check this channel out. Not quite what I was expecting. I have seen the gist of this topic explained in at least a dozen stackoverflow threads. So why the constant bombardments of on-site training? It's such a clear topic to explain through code example.

  • @hasanrasulov3502
    @hasanrasulov3502 7 днів тому

    Hello, Jason. I like your explanations. I’d be happy if you made a video about “how undefined behaviours is useful for optimizations”.

    • @cppweekly
      @cppweekly 6 днів тому

      I have a place for viewers to request topics so I don't lose track of them: github.com/lefticus/cpp_weekly/issues/ Feel free to add your idea and vote on the others on the list!

    • @hasanrasulov3502
      @hasanrasulov3502 6 днів тому

      Cool

  • @nicholaskomsa1777
    @nicholaskomsa1777 8 днів тому

    so yeah, you can use patterns from networking server/client model for instance in #include <boost/asio.hpp> which I did, and then a thread-safe message std::deque. Throw in some templates, job basically done. btw, these messages can also transverse the ol' internet.

  • @BobruiskChessStar
    @BobruiskChessStar 8 днів тому

    With this method you also don't have to worry about the moved-from stack variables l1 or l2 being, in a practical sense, invalid after the point of the move.

  • @konstantin2296
    @konstantin2296 8 днів тому

    If there a way to reuse that lambda?

    • @cppweekly
      @cppweekly 2 дні тому

      Certainly, just have a function that creates and returns the lambda, or copy the lambda (and its state) at any point along the way.

    • @konstantin2296
      @konstantin2296 2 дні тому

      @@cppweekly yeah, exactly what I did. But hoped maybe there is a trick to default construct mutable lambda without factory function

  • @nicholaskomsa1777
    @nicholaskomsa1777 8 днів тому

    I would argue that if you are encountering these race conditions or thinking about how they work, then you could use refactoring instead.

    • @cppweekly
      @cppweekly 2 дні тому

      If you use global statics, you have to worry about this. If you have interactions between static and thread_local, you have to worry about this.

    • @nicholaskomsa1777
      @nicholaskomsa1777 День тому

      @@cppweekly yeah, but then you also have to resolve it all somehow. Probably by refactoring out of the race-conditions to start.

  • @nicholaskomsa1777
    @nicholaskomsa1777 9 днів тому

    When I write performant cutting edge code, I use "OOP" less than I use "DOD". Fundamentally different hemispheres of code concepts.

  • @nicholaskomsa1777
    @nicholaskomsa1777 9 днів тому

    all of this seems outdated by instead using concepts of BOOST/STL/algorithms/lambdas/std::function/views. I don't know, I think this methodology that you use may be outdated.

  • @nicholaskomsa1777
    @nicholaskomsa1777 9 днів тому

    padding issues are solved largely by Structures of Arrays programming method, speed increase even more dramatic than worrying about complicated structure concepts.

  • @nicholaskomsa1777
    @nicholaskomsa1777 9 днів тому

    first of all, I'd more than likely be using items like std::uint16_t or std::uint32_t, and then std::size_t. Your code appears depreciated.

    • @cppweekly
      @cppweekly 2 дні тому

      Not at all - size_t means "native machine size type" this is the correct thing to do for crossplatform code. int32, etc, should be used when I want to specify the exact type right now.

    • @nicholaskomsa1777
      @nicholaskomsa1777 День тому

      @@cppweekly Rather than using c-style data types that aren't quite so clear about their properties, which you describe in your video. Instead, if you want to clarify them - which is always - you use std data types and constructs. std::size_t is not designed to be portable, it's supposed to be the largest possible unsigned integer-type the system supports. std::size_t is entirely system dependent by design.

  • @nicholaskomsa1777
    @nicholaskomsa1777 9 днів тому

    Hello, fellow C++ enthusiast, please learn to do this instead: struct LifetimeWriter { int value{ 0 }; LifetimeWriter( int value) :value(value) { std::cout << "constructor" << std::endl; } ~LifetimeWriter() { std::cout << "destructor" << std::endl; } LifetimeWriter(const LifetimeWriter& other) { std::cout << "copy constructor" << std::endl; } LifetimeWriter(LifetimeWriter&& other) { std::cout << "move constructor" << std::endl; } LifetimeWriter& operator=(const LifetimeWriter& other) { std::cout << "copy assignment" << std::endl; return *this; } LifetimeWriter& operator=(LifetimeWriter&& other) { std::cout << "move assignment" << std::endl; return *this; } }; std::array<LifetimeWriter, 2 > arr = { LifetimeWriter{5}, {7} };

  • @shawnzhong
    @shawnzhong 9 днів тому

    Can’t you use std::views::repeat instead of iota?

    • @cppweekly
      @cppweekly 2 дні тому

      umm.... maybe? I didn't think about that.

  • @hammad7829
    @hammad7829 9 днів тому

    how do you type so fast??

  • @Dimkar3000
    @Dimkar3000 9 днів тому

    Is it just me or this code is unreadable? A lambda wrapped in a generator function that uses 2 stl functions to pipe the values the result of the lambda around so we avoid compilation errors...

  • @Fleshbits1
    @Fleshbits1 10 днів тому

    Jason, you need to either put something on your mic, play with eq and gates, or practice some speech exercises, as you are suffering from the common "wet mouth noises" in your audio, which tend to drive some people batshit. Just wanted to let you know in case you weren't aware.

  • @thisisolorin394
    @thisisolorin394 10 днів тому

    The typical use case would be to have a custom generator. Here that is essentially provided by iota

  • @XzatonJW
    @XzatonJW 10 днів тому

    You could check if the invocable is callable with the iota output and if not wrap the callable in another lambda

  • @RishabhDeepSingh
    @RishabhDeepSingh 10 днів тому

    Damnnn this is high level c++

  • @anon_y_mousse
    @anon_y_mousse 11 днів тому

    This is an interesting method, but requires too much boilerplate for my liking. I also don't think I'll ever like `views` or any of the functional programming constructs that they've added to C++.

    • @cppweekly
      @cppweekly 2 дні тому

      I don't consider reusable functions to be "boilerplate" the generator function can be used with any callable you pass into it.

    • @anon_y_mousse
      @anon_y_mousse 2 дні тому

      @@cppweekly Just because something is reusable doesn't mean it's not still boilerplate. Consider how many other languages there are that make it easy to one-line such a construct.

  • @Pelfik
    @Pelfik 11 днів тому

    This shit is much less readable than a normal c++ code. What is its purpose?

    • @fuxorfly
      @fuxorfly 11 днів тому

      They are reusable components that allow you to composably construct more complicated views of data. Say you wanted to take a vector, skip the first N number of elements in it, and then call a function on the next M number of elements on it. With a composable view, that becomes as simple as: for(auto i : v | std::views::drop(numSkipFirst) | std::views::take(numToUseAfter)) { func(i); } If you wanted to do that without views, you would need an index variable, keep track of some math, and make sure you are indexing and iterating the iterators correctly. Its a lot more complicated: auto i = v.begin(); std::advance(i, numSkipFirst); for(auto index = numSkipFirst; index < (numSkipFirst+numToUseAfter) and i != v.end(); ++i, ++index) { func(*i); } As soon as your needs get out of the realm of the toy slide-ware example, it becomes significantly more readable to use ranges and views.

    • @anon_y_mousse
      @anon_y_mousse 10 днів тому

      @@fuxorfly To be perfectly honest, both examples look like garbage and if the language treated arrays as first class objects it'd be a lot simpler without such disgusting code. Consider a language where they are first class and you could have: for i in a[N:M] { fun( i ); }. The meaning of which would be taking M elements from N forward, iterating that subarray and calling fun() on each element in turn.

    • @fuxorfly
      @fuxorfly 10 днів тому

      @@anon_y_mousse you don't need arrays to be first class objects to get syntax like that - views are the way to enable that. if you want to combine multiple view adapters into a single thing that is more concise you can. that's part of what makes view adaptors powerful. the other part is that you can use them with far more than just first class types; in this case, user defined types are enabled to be equally as powerful as language provided ones. example here is a vector, but it could be a map, a stream iterator, etc.

    • @anon_y_mousse
      @anon_y_mousse 10 днів тому

      @@fuxorfly It's added syntax that achieves a partial result of arrays being treated as first class objects. Keep in mind that you couldn't do range based for loops prior to C++11. Also keep in mind that a language treating something as a first class object just means that there's syntax added to enable easier usage of it as a concept. We still can't do [X:Y] syntax to acquire a range, nor can we do [X..Y] either, but if they add that syntax then the first class treatment will be complete.

  • @wojciechrazik
    @wojciechrazik 11 днів тому

    Nice exampleI think it would be worth to benchmark this with coroutine version!

    • @konstantin2296
      @konstantin2296 8 днів тому

      You can do that but I can't even imagine a way how coroutines could be faster here. Clang literally optimizes it to print 10 fib numbers line by line.

    • @wojciechrazik
      @wojciechrazik 8 днів тому

      @@konstantin2296 Exactly! I would rather expect coroutine version to be slower. That's why it would great to measure it!

    • @cppweekly
      @cppweekly 2 дні тому

      Yes, it would likely be orders of magnitude faster than the equivalent coroutine. you've probably already see the follow up episode now about that.

  • @cgazzz
    @cgazzz 11 днів тому

    It looks like only infinite generators can be done this way. Python uses GeneratorExit exception to dynamically end the generator, but exceptions shouldn't be used like that in C++