Andreas Fertig
Andreas Fertig
  • 61
  • 69 846
C++ Insights - Episode 58: C++ Insights new show object lifetime transformation
In this episode, you learn about a new transformation in C++ Insights that visualizes object lifetime. This can help you uncover UB. I'll demonstrate the power of the transformation using range-based for-loops and their latest update in C++23.
Source code: cppinsights.io/s/6b06f7eb
---
WORK WITH ME
I'm available for in-house C++ training classes, onsite or remote. You can also check out my self-study courses below.
► My training classes: andreasfertig.com/courses/
‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️
Training Classes
- "Safe and Efficient C++ for Embedded Environments", C++Online, February 24, cpponline.uk/workshop/safe-and-efficient-cpp-for-embedded-environments/
Talks
- "Fast and small C++ - When efficiency matters", C++Online, February 27
STAY CONNECTED
► Bluesky: bsky.app/profile/andreasfertig.com
► LinkedIn: linkedin.com/in/andreasfertig
► Mastodon: mas.to/@andreasfertig
► GitHub: github.com/andreasfertig
► Newsletter: andreasfertig.com/newsletter/
ANDREAS' BOOKS
► Amazon (Paperback): amzn.com/stores/author/B01IDTFSRE
► Leanpub (Ebook): leanpub.com/u/andreasfertig
ANDREAS' SELF-STUDY COURSES
► A day with C++20s Coroutines: fertig.to/ytslcoro
► Programming with C++17 and C++20: fertig.to/ytslcpp20
ANDREAS' CONFERENCE VIDEOS
► th-cam.com/play/PLm0Dc2Lp2ycYeLZEYqWSsCch7mZ0qP9Xi.html
MORE C++ INSIGHTS VIDEOS
► th-cam.com/play/PLm0Dc2Lp2ycaFyR2OqPkusuSB8LmifY8D.html
SUPPORT THE PROJECT
► GitHub Sponsors: github.com/sponsors/andreasfertig
► Patreon: www.patreon.com/cppinsights
C++ Insights online: cppinsights.io
#cppinsights #cpp #cpp23
มุมมอง: 506

วีดีโอ

C++ Insights - Episode 57: More performance thanks to more implicit moves in C++20
มุมมอง 759หลายเดือนก่อน
In this episode, you learn about a change in the standard that can give you more performance, thanks to the implicit move. Source code: godbolt.org/z/r1W4K9oGM ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️ Talks - "C 20 Coroutinen - Ein Einstieg", ADC, May 07 Training Classes - "C 20 Coroutinen - Ein Einstieg", ADC, May 06, adc.ms/24/Workshops/ WORK WITH ME ► My training classes: a...
C++ Insights - Episode 56: Understanding the magic behind C++17's structured bindings
มุมมอง 7632 หลายเดือนก่อน
In this episode, you learn how C 17s structured bindings work. Source code: cppinsights.io/s/1c69deaf WORK WITH ME I'm available for in-house C training classes, onsite or remote. You can also check out my self-study courses below. ► My training classes: andreasfertig.com/courses/ ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️ Talks - "Fast and small C - When efficiency matters", Me...
C++ Insights - Episode 55: References are no pointers!
มุมมอง 1.2K3 หลายเดือนก่อน
In this episode, you learn the difference between references and pointer. Yes, there is one! Source code: compiler-explorer.com/z/s4cd47bnh
C++ Insights - Episode 54: You can't force a move!
มุมมอง 6744 หลายเดือนก่อน
In this episode, I show you why you can't force a move and the consequences. Source code: cppinsights.io/s/7079ad3a WORK WITH ME I'm available for in-house C training classes, onsite or remote. You can also check out my self-study courses below. ► My training classes: andreasfertig.com/courses/ ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️ Talks - "Fast and small C - When efficienc...
C++ Insights - Episode 53: Mastering C++23: Leveraging if consteval for more constexpr functions
มุมมอง 5435 หลายเดือนก่อน
In this episode, you learn about yet another constexpr piece, if consteval, from C 23. I continue where I left off last time and complete the constexpr Assert function. Source code: compiler-explorer.com/z/7joqaoYG6 WORK WITH ME I'm available for in-house C training classes, onsite or remote. You can also check out my self-study courses below. ► My training classes: andreasfertig.com/courses/ ‼...
C++ Insights - Episode 52: The Power of std::is_constant_evaluated(): Elevating Your Constexpr Code
มุมมอง 5856 หลายเดือนก่อน
In this episode, you learn about another constexpr piece, std::is_constant_evaluated from C 20. I will show you how std::is_constant_evaluated helps you to write even more constexpr code. Source code: compiler-explorer.com/z/9zEMhejob WORK WITH ME I'm available for in-house C training classes, onsite or remote. You can also check out my self-study courses below. ► My training classes: andreasfe...
C++ Insights - Episode 51: The magic of C++20s std::source_location
มุมมอง 2.2K7 หลายเดือนก่อน
In this episode, you learn about std::source_location and how its static member function current works. Source code: compiler-explorer.com/z/sd5Ef64a8 WORK WITH ME I'm available for in-house C training classes, onsite or remote. You can also check out my self-study courses below. ► My training classes: andreasfertig.com/courses/ ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️ Talks -...
C++ Insights - Episode 50: What are the static operators [] and () in C++23
มุมมอง 7648 หลายเดือนก่อน
In this episode, you learn about the two new static operators [] and () ind C 26. Source code: godbolt.org/z/6hYzosbdE WORK WITH ME I'm available for in-house C training classes, onsite or remote. You can also check out my self-study courses below. ► My training classes: andreasfertig.com/courses/ ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️ Talks - "Fast and small C - When effici...
C++ Insights - Episode 49: Why you shouldn't have virtual member functions with default parameters
มุมมอง 1.1K9 หลายเดือนก่อน
In this episode, you learn how default parameters work and why you shouldn't put them on virtual member functions. Source code: cppinsights.io/s/1e0e7ae0 ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️ Training Classes - "Modern C : When Efficiency Matters", CppCon, September 21 - 22, cppcon.org/class-2024-when-efficiency-matters/ Talks - "Fast and small C - When efficiency matters",...
C++ Insights - Episode 48: Less typing for lambdas in C++23
มุมมอง 78510 หลายเดือนก่อน
In this episode, you learn about a C 23 change that makes you type even less when creating lambdas. Source code: cppinsights.io/s/af3f21a3 ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️ Talks - "Fast and small C - When efficiency matters", CppCon, September 17 - "Back to Basics: Object-Oriented Programming", CppCon, September 20 - "Fast and small C - When efficiency matters", code::...
C++ Insights - Episode 47: Prefer user-defined literals when available
มุมมอง 82711 หลายเดือนก่อน
In this episode, you learn the benefits of user-defined literals. Source code: cppinsights.io/s/59d25e54 ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️ Talks - "C 20 Coroutinen - Ein Einstieg", ADC, May 07 Training Classes - "C 20 Coroutinen - Ein Einstieg", ADC, May 06, adc.ms/24/Workshops/ WORK WITH ME ► My training classes: andreasfertig.com/courses/ STAY CONNECTED ► Bluesky: bsk...
C++ Insights - Episode 46: Consider making your assignment operators lvalue only
มุมมอง 1.1Kปีที่แล้ว
In this episode, you learn how reference qualifiers will make your code more robust. Source code: compiler-explorer.com/z/1qzEMEdxT WORK WITH ME I'm available for in-house C training classes, onsite or remote. You can also check out my self-study courses below. ► My training classes: andreasfertig.com/courses/ ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️ Talks - "Fast and small C ...
C++ Insights - Episode 45: C++20s Designated Initializers
มุมมอง 730ปีที่แล้ว
In this episode, you learn about C 20s designated initializers and how to use them. Source code: cppinsights.io/s/e8618d80 WORK WITH ME I'm available for in-house C training classes, onsite or remote. You can also check out my self-study courses below. ► My training classes: andreasfertig.com/courses/ ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ‼️ Talks - "Fast and small C - When ef...
C++ Insights - Episode 44: Compilers are lazy, and this is good
มุมมอง 693ปีที่แล้ว
In this episode, you learn that compilers are lazy, for example, when generating special member functions, which is good. Source code: cppinsights.io/s/48e2e9de WORK WITH ME I'm available for in-house C training classes, onsite or remote. You can also check out my self-study courses below. ► My training classes: andreasfertig.com/courses/ ‼️ 📅 🗓️ UPCOMING OPEN-ENROLLMENT CLASSES AND TALKS 🗓️ 📅 ...
C++ Insights - Episode 43: A copy constructor cannot be a template
มุมมอง 1.1Kปีที่แล้ว
C Insights - Episode 43: A copy constructor cannot be a template
🇩🇪 ESE Kongress 2020: C++ Insights: Sehen Sie Ihren Quellcode mit den Augen eines Compilers
มุมมอง 141ปีที่แล้ว
🇩🇪 ESE Kongress 2020: C Insights: Sehen Sie Ihren Quellcode mit den Augen eines Compilers
C++ Insights - Episode 42: C++20: When is a struct an aggregate
มุมมอง 2Kปีที่แล้ว
C Insights - Episode 42: C 20: When is a struct an aggregate
🇩🇪 ESE Kongress 2022: C++ 20 Templates - Die nächste Generation
มุมมอง 198ปีที่แล้ว
🇩🇪 ESE Kongress 2022: C 20 Templates - Die nächste Generation
C++ Insights - Episode 41: How type-traits work
มุมมอง 4.1Kปีที่แล้ว
C Insights - Episode 41: How type-traits work
C++ Insights - Episode 40: The traps of returning references
มุมมอง 1.1Kปีที่แล้ว
C Insights - Episode 40: The traps of returning references
C++ Insights - Episode 39: Recursion-free templates with C++17s fold expressions
มุมมอง 1.3Kปีที่แล้ว
C Insights - Episode 39: Recursion-free templates with C 17s fold expressions
C++ Insights - Episode 38: User-provided vs. user-declared
มุมมอง 1Kปีที่แล้ว
C Insights - Episode 38: User-provided vs. user-declared
C++ Insights - Episode 37: C++20's range-based for-loop with initializer
มุมมอง 921ปีที่แล้ว
C Insights - Episode 37: C 20's range-based for-loop with initializer
C++ Insights - Episode 36: Coroutine customization points
มุมมอง 463ปีที่แล้ว
C Insights - Episode 36: Coroutine customization points
C++ Insights - Episode 35: C++20's Coroutines: One thing you should watch out for
มุมมอง 516ปีที่แล้ว
C Insights - Episode 35: C 20's Coroutines: One thing you should watch out for
C++ Insights - Episode 34: C++20: Captureless lambdas in unevaluated contexts
มุมมอง 2742 ปีที่แล้ว
C Insights - Episode 34: C 20: Captureless lambdas in unevaluated contexts
C++ Insights - Episode 33: C++17's Class Template Argument Deduction
มุมมอง 6212 ปีที่แล้ว
C Insights - Episode 33: C 17's Class Template Argument Deduction
C++ Insights - Episode 32: Captureless lambdas with a static invoker
มุมมอง 3482 ปีที่แล้ว
C Insights - Episode 32: Captureless lambdas with a static invoker
C++ Insights - Episode 31: Visualize padding with C++ Insights
มุมมอง 9282 ปีที่แล้ว
C Insights - Episode 31: Visualize padding with C Insights

ความคิดเห็น

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

    ohh man, these are magic...

  • @SamWhitlock
    @SamWhitlock 11 วันที่ผ่านมา

    I hadn't know about this part of C++ Insights. Neat! Are there any other areas in C++ where this "anti-dangle" stuff is performed? If you did `get().items()` outside of this context, you'd still get a dangling ref, e.g. `auto &items = get().items(); auto b = items.begin(); // kaboom here`. I understand practically why this behavior is special-cased for the range for loop, but is this the only context in which it is done?

  • @ask43242flight
    @ask43242flight หลายเดือนก่อน

    How do I use a third-party library (i.e. simdjson library) on c++ insights website? Thank you

  • @sanjaygatne1424
    @sanjaygatne1424 หลายเดือนก่อน

    फाॅन्ट आकाराने लहान आहे. वाचायला त्रास होतो.

  • @almondengine
    @almondengine หลายเดือนก่อน

    I didn't know that trick or tool, now I do thank you!

  • @EddingDefault
    @EddingDefault 2 หลายเดือนก่อน

    Pedagogically speaking, I think that the analogy in the beginning can be clarified by saying that references and pointers are similar because they are both mechanisms of indirection. They are different, because the language doesn't treat them as assignable values. This separation of dimensions of likeness and difference maps onto the performance implications: indirection requires references to be represented as addresses in CPU in all but the most contrived usage scenario, so just like for pointers the indirection cost is there. But transient references don't need their own storage and can indeed ever only be in registers, which differs from pointers.

  • @iuiui-iuiui
    @iuiui-iuiui 2 หลายเดือนก่อน

    If you claim performance, show a benchmark! I'd say user strings are not long enough to note any significant difference

  • @iuiui-iuiui
    @iuiui-iuiui 2 หลายเดือนก่อน

    Am I the only one who is mesmerised by the std::basic_format_string template arguments mimicking the other arguments to std::println? What kind of sorcery is that?

    • @AlfredoCorrea
      @AlfredoCorrea 2 หลายเดือนก่อน

      I think this is because std::println deduces the second and third argument and imposes these template arguments into its first argument. Cppinsights shows this argument type of the called function because there is a conversion involved.

  • @AlfredoCorrea
    @AlfredoCorrea 2 หลายเดือนก่อน

    I guess this can bring some clarity in which cases the program will do (or not do) life-time extension with `auto const&` and `auto&&`. Very nice.

  • @sanjaygatne1424
    @sanjaygatne1424 2 หลายเดือนก่อน

    सी ++ इनसाईट बहोत ही उपयुक्त है.

  • @McDonaldIbekwe
    @McDonaldIbekwe 2 หลายเดือนก่อน

    Nice video with very clear explanation.

  • @MarcosCpp
    @MarcosCpp 3 หลายเดือนก่อน

    I liked this video, quick and straight to the point

  • @KalkiCharcha-hd5un
    @KalkiCharcha-hd5un 3 หลายเดือนก่อน

    Your vids are unreadable on mobile screen size I wish it had bigger fonts.

    • @iuiui-iuiui
      @iuiui-iuiui 2 หลายเดือนก่อน

      Try pinch to zoom👌

  • @xorbe2
    @xorbe2 3 หลายเดือนก่อน

    Need to differentiate reference as a local variable vs an object member variable. Locally it can just be a name alias effectively. In an object, it's basically a pointer and you can actually get hacky and change what the reference references.

  • @gehirndoper
    @gehirndoper 3 หลายเดือนก่อน

    Interesting, but I think in most cases this does not make a difference: If the pointer is only used locally, the compiler should optimise storing it away if possible. Conversely if you have a class which stores a reference to something, this would take the same space as storing a pointer.

    • @imranjavaid71
      @imranjavaid71 3 หลายเดือนก่อน

      For your first point of compiler optimizing it away, I don't think that is true. You can test that be adding "-O3" to the compiler-explorer code in the description. Second point is correct. The reference will take space in a class.

    • @gehirndoper
      @gehirndoper 3 หลายเดือนก่อน

      @@imranjavaid71 You are diplaying the address of the pointer, so this is not a relevant example.

  • @tomkirbygreen
    @tomkirbygreen 3 หลายเดือนก่อน

    Great video sir. I think you might want to consider changing ‘no’ to ‘not’ in the title.

    • @TheMR-777
      @TheMR-777 3 หลายเดือนก่อน

      That's actually an accented way of saying it :)

    • @andreas_fertig
      @andreas_fertig 3 หลายเดือนก่อน

      Thanks! I think its to late to change.

  • @valentinsiltsenko
    @valentinsiltsenko 3 หลายเดือนก่อน

    Thank you for the great explanation!

  • @Luckygangff
    @Luckygangff 3 หลายเดือนก่อน

    hi sir i wrote a generic code for stl containers but cpp insights it is not compiling can you please help me to solve it #include<iostream> #include <list> #include<vector> template<typename T,template<typename> class X> void print(X<T> &container){ for(auto &i: container) { std::cout<<i<<" , "; } std::cout<<" "; } int main(){ std::vector<int> a{1,2,3,4,5,6}; std::list<double> b{1.5,2.5,3.5,4.5,5.5,6.5}; print(a); print(b); return 0; }

  • @garyp.7501
    @garyp.7501 4 หลายเดือนก่อน

    That is a good tip if you want to make your assignments always use a reference to the object.

  • @rafalmichalski4893
    @rafalmichalski4893 4 หลายเดือนก่อน

    Why do we need this cast as const & binds to everything - even to non-const && (which is returned from std::move) ?

  • @AlfredoCorrea
    @AlfredoCorrea 4 หลายเดือนก่อน

    Why CppInsights puts a static_cast to const Apple&& and not to const Apple&, which is the argument of the copy constructor? Great video!

  • @mlavik1
    @mlavik1 4 หลายเดือนก่อน

    Great video! Is it possible to get a warning for this somehow? (though Clang Tidy or something). If I do std::move explicitly I wouldn't want it to be copied, at least not in non-template code. These kind of issues can be really had to spot :'D

  • @sebastienlevy8127
    @sebastienlevy8127 4 หลายเดือนก่อน

    Excellent example for if consteval, outlining with a practical use case the difference with std::is_constant_evaluated. It builds a clear bridge between the pure compile-time world and the hybrid compile-time/run-time world represented byt constexpr context. Great video, keep up the good work!

  • @tomkirbygreen
    @tomkirbygreen 5 หลายเดือนก่อน

    Awesome video. I don’t know if you have any interest or ability to edit the presentation at this point but there’s a missing ‘h’ in ‘tings’.

    • @andreas_fertig
      @andreas_fertig 5 หลายเดือนก่อน

      Thanks! Unfortunately, I can't edit/update the already published video :-)

  • @adeelahmad9875
    @adeelahmad9875 6 หลายเดือนก่อน

    Nice !!

  • @panjak323
    @panjak323 6 หลายเดือนก่อน

    So why isn't noexcept the default?

    • @andreas_fertig
      @andreas_fertig 6 หลายเดือนก่อน

      Well, because it was introduced a long time after C++98. In addition it is a save default. Once you start throwing exceptions which pass functions which are noexcept default things get hard.

  • @garyp.7501
    @garyp.7501 6 หลายเดือนก่อน

    Of course now I want to use a Macro to fill in the boiler plate code. #define LogMyError(x) LogError(std::operator""sv(x, 11UL), std::source_location::current(__buildin_source_location())) LogMyError("Just a test);

    • @andreas_fertig
      @andreas_fertig 6 หลายเดือนก่อน

      Sarcasm? I don't get why you would want to use a macro and why use __builtin_source_location() in ::current. The compiler does that for you already.

    • @garyp.7501
      @garyp.7501 6 หลายเดือนก่อน

      @@andreas_fertig Then I was confused at 4:50 or so where you used it. The define is to avoid the extra typing or copy/pasting of lines 14, 15 of the same code in your right side window.

    • @nielsdegroot9138
      @nielsdegroot9138 3 หลายเดือนก่อน

      @@garyp.7501 You don't need to do that. The right side shows what the compiler makes of it behind the scenes.

  • @anjumlatif3052
    @anjumlatif3052 6 หลายเดือนก่อน

    Video voice quality is not good.

    • @andreas_fertig
      @andreas_fertig 6 หลายเดือนก่อน

      Sorry about that!

    • @almondengine
      @almondengine หลายเดือนก่อน

      so lets earn him a $5000 mic!

  • @zeez7777
    @zeez7777 7 หลายเดือนก่อน

    NIce

  • @tomkirbygreen
    @tomkirbygreen 7 หลายเดือนก่อน

    Very useful. Seems like it’s far from zero cost. Does noexcept get used much in (semi) realtime systems?

    • @andreas_fertig
      @andreas_fertig 6 หลายเดือนก่อน

      Your move-constructors and move-assignment operators are typically noexcept. That's the reason why we have noexcept in the first place.

  • @davithov
    @davithov 8 หลายเดือนก่อน

    First, thank you very much for the great lecture! I have a question: so we use integral_constant<bool, true> type, but isn't it possible somehow provide only value, i.e., for example `true`? Because we know, right, that `true` (or `false`) are of type bool? So IMO, it will be more natural to find a way to provide only values. Maybe should we use function templates to be able to deduce type? But, on the other hand, if I am not mistaken, starting from c++17 classes also deduce type, so maybe somehow can we use this?

    • @lev-th
      @lev-th 7 หลายเดือนก่อน

      Difference is that integral_constant<bool, true> and integral_constant<bool, false> are of different types.

    • @andreas_fertig
      @andreas_fertig 6 หลายเดือนก่อน

      With C++17 we can have "auto" as a non-type template parameter. This allows you to use a single parameter cppinsights.io/s/39165dba

    • @davithov
      @davithov 6 หลายเดือนก่อน

      @@andreas_fertig That's fantastic! Thank you for the answer )

  • @davithov
    @davithov 8 หลายเดือนก่อน

    So, in line 13 it is written min(m, ts....); Does that mean that at each step we reduce number of params in ts by 1 and pass it to 'b' ? it is also a bit confusing for me where exactly to put '...' . For example, why it is wrong to write like this: template<typename T ... >

    • @andreas_fertig
      @andreas_fertig 8 หลายเดือนก่อน

      Yes, we reduce the number of parameters by merging the minimum of a and b into a. Then, we grab the first parameter of the pack and pass it to b, reducing the number of parameters in the pack by one in each iteration. To your second part, it is convention. The ellipsis goes right to the entity it belongs to. In your example, the typename.

    • @davithov
      @davithov 8 หลายเดือนก่อน

      @@andreas_fertig Thank you very much for personally answering to my question! It is an honour for me.

  • @PaulTopping1
    @PaulTopping1 9 หลายเดือนก่อน

    A default parameter on a function in the base class is still useful. It allows a no-argument call to have meaning throughout the class hierarchy. It seems like the real problem here is the compiler should not allow a different default value for the virtual function in a derived class.

  • @JaswinderSingh-dz1ui
    @JaswinderSingh-dz1ui 9 หลายเดือนก่อน

    Great

  • @phusicus_404
    @phusicus_404 10 หลายเดือนก่อน

    You can use #pragma pack to make members adjacent, but work with memory will be slower.

  • @ЮрійГордієнко-х2з
    @ЮрійГордієнко-х2з 10 หลายเดือนก่อน

    Wrong

  • @JustABigFan2010
    @JustABigFan2010 11 หลายเดือนก่อน

    Andrea 99% of the surface area of your video content is blank. And the most important parts of your video is in the top left corner which is only 1% of the area. Would you please on your next videos kind of focus on that 1%. Either make the font larger so that it kind of becomes the 50% of the video surface area. A lot of people with huge monitors make this mistake. We don't need to see the useless 99% of the blank area. For the people too comfortably see that 1% of the surface area they will need to buy large monitors same as you. So watching this video on a 15 inch PC monitor a tablet or a phone is basically useless because you cannot even Zoom in on TH-cam videos.

    • @phusicus_404
      @phusicus_404 10 หลายเดือนก่อน

      You can zoom a video on TH-cam on a phone.

    • @JustABigFan2010
      @JustABigFan2010 10 หลายเดือนก่อน

      @@phusicus_404 Write and we love reading source code on a 6 inch phone screen. Besides do you think the quality when you zoom in is going to be good enough to understand anything that you're reading in the source code. Don't be silly.

  • @AlfredoCorrea
    @AlfredoCorrea 11 หลายเดือนก่อน

    Didn't think about this before. The key is also that std::string s{"Hell\0o"}; is not the same as std::string s = "Hell\0o"s; .

    • @andreas_fertig
      @andreas_fertig 11 หลายเดือนก่อน

      That's a good point! However, the std::string consumer must respect the string size, not the null terminator.

  • @zuofu-ziv
    @zuofu-ziv 11 หลายเดือนก่อน

    binge watching the series

    • @andreas_fertig
      @andreas_fertig 11 หลายเดือนก่อน

      Awesome! Sometimes, I forget that I've published several episodes.

  • @zuofu-ziv
    @zuofu-ziv 11 หลายเดือนก่อน

    very insightful!

  • @reflec8525
    @reflec8525 11 หลายเดือนก่อน

    thanks interesting insights. It seems initializer lists are not designed to pass by reference. Two other cases that are maybe more optimal if you avoid lvalues... Use move (if you have a move ctor): UInit c{std::move(a), std::move(b)}; Use temporaries to make the init list. maybe more the original thinking behind the concept: Uinit d{Unit(), Unit()}; //default, default, init list

    • @andreas_fertig
      @andreas_fertig 11 หลายเดือนก่อน

      I agree, initializer lists are not designed to pass by reference. They are a view and not a container.

  • @David_Friberg
    @David_Friberg ปีที่แล้ว

    Note that this technique may unexpectedly break the rule of zero, in the cases where it would otherwise apply. As you declare the copy assignment op (even when explicitly defining it as defaulted) you suppress the implicit declaration of a defaulted move ctor and defaulted a move assignment operator. A mitigation comes with a cost: adding additional boilerplate code to bring us into rule of five, even if all special members are explicitly defined as defaulted. As an additional niche pitfall, it may also make aggregates into non-aggregates from C++20 and onward, as we need only user-declare a ctor to disqualify the class as an aggregate. Whilst this technique makes sense semantically, syntactically it can add a lot of noise, particularly in environments (automotive, safety-critical) where rule guidelines such as AUTOSAR C++14 (rule A12-0-1) will enforce rule of five if you cannot stick to rule of zero. Whilst this seems to have been addressed in MISRA C++:2023, I'm still leaning towards mitigation for to-rvalue assignment being enforced by static analysis (fully decidable) instead of via a language measure that comes with this kind of cost.

  • @VincentZalzal
    @VincentZalzal ปีที่แล้ว

    Have you seen any downsides to doing this? Does it prevent some common code idiom? I wonder if this could be made into a general guideline.

    • @David_Friberg
      @David_Friberg ปีที่แล้ว

      There are some downsides to this, but potentially the benefit holds their weight. See my comment above for details.

    • @VincentZalzal
      @VincentZalzal ปีที่แล้ว

      @@David_Friberg For some reason, I don't see your comment.

    • @davidf7196
      @davidf7196 ปีที่แล้ว

      @@VincentZalzal Seems TH-cam's overzealously censored my comment, and does so even when I try to repost it. Trying from another account. What I wrote was: Note that this technique may unexpectedly break the rule of zero, in the cases where it would otherwise apply. As you declare the copy assignment op (even when explicitly defining it as defaulted) you suppress the implicit declaration of a defaulted move ctor and defaulted a move assignment operator. A mitigation comes with a cost: adding additional boilerplate code to bring us into rule of five, even if all special members are explicitly defined as defaulted. As an additional niche pitfall, it may also make aggregates into non-aggregates from C++20 and onward, as we need only user-declare a ctor to disqualify the class as an aggregate. Whilst this technique makes sense semantically, syntactically it can add a lot of noise, particularly in environments (automotive, safety-critical) where rule guidelines such as AUTOSAR C++14 (rule A12-0-1) will enforce rule of five if you cannot stick to rule of zero. Whilst this seems to have been addressed in MISRA C++:2023, I'm still leaning towards mitigation for to-rvalue assignment being enforced by static analysis (fully decidable) instead of via a language measure that comes with this kind of cost.

    • @David_Friberg
      @David_Friberg ปีที่แล้ว

      @@VincentZalzal For some reason my other (explanatory) comment keeps getting censored by TH-cam, no idea why. Instead, I posted it as a Q&A on r/cpp_questions, hopefully you can find it there.

    • @VincentZalzal
      @VincentZalzal ปีที่แล้ว

      @@David_FribergFound it, thanks.

  • @stanbarvinsky1011
    @stanbarvinsky1011 ปีที่แล้ว

    great tip! thank you

  • @TheBuilder
    @TheBuilder ปีที่แล้ว

    I'll think about it

  • @valentinsiltsenko
    @valentinsiltsenko ปีที่แล้ว

    Great tip. Thanks a lot!

  • @ggladilov
    @ggladilov ปีที่แล้ว

    Is it the same with -O3? What does destructing an empty object mean? Would empty struct optimisation interfere here?

    • @andreas_fertig
      @andreas_fertig ปีที่แล้ว

      The example is too simple for the compiler with -O3. Still, in a code base that does something with the optional, it depends on the compiler's ability to see through it. In the worst case, the compiler performs a call to an empty destructor. That might not cost much, but the costs increase if it affects caching because the destructor is not in the hot cache.

  • @jaime7295
    @jaime7295 ปีที่แล้ว

    why C++ is so fucking cmplex XDD?

  • @AlfredoCorrea
    @AlfredoCorrea ปีที่แล้ว

    I don’t get it, the compiler can demonstrate that the body of the function will not be able to throw, so it will not contemplate calling terminate. So the code will be optimal in this case anyway. This video gives the opposite impression because it doesn’t say that all this will be optimized. I think the problem is when you say no except and you are calling a function that the compiler cannot demonstrate it cannot throw. In this case the compiled code generated will generate the call to terminate. I heard that there was an old argument in the committee and there was a faction (Abrahams?) that wanted plain UB for uncatched throwing inside noexcept and the other wanted a guaranteed call to terminate. The second faction won but there were some regrets afterwards.

    • @andreas_fertig
      @andreas_fertig ปีที่แล้ว

      There is no guarantee that an optimizer must do a particular optimization. This simple example compiler-explorer.com/z/TGeWcxrcf leads to code the compiler cannot efficiently optimize even though Other could simply return 2.

  • @mohammadghasemi2402
    @mohammadghasemi2402 ปีที่แล้ว

    Thank you. Are the contents of your videos also applicable to MSVC?

    • @andreas_fertig
      @andreas_fertig ปีที่แล้ว

      As far as I know, yes. It's mostly about work the compiler has to do, which results in longer compile times. We want fast compilers. A compiler that deliberately creates non-required objects just so the optimizer can throw them away will not be as fast as the one that creates only what is required.