Andreas Fertig
Andreas Fertig
  • 60
  • 66 390
C++ Insights - Episode 57: More performance thanks to more implicit moves in C++20
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: andreasfertig.com/courses/
STAY CONNECTED
► Bluesky: bsky.app/profile/andreasfertig.com
► Twitter: Andreas__Fertig
► Mastodon: mas.to/@andreasfertig
► LinkedIn: linkedin.com/in/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' CONFERENCE VIDEOS
► ua-cam.com/play/PLm0Dc2Lp2ycYeLZEYqWSsCch7mZ0qP9Xi.html
MORE C++ INSIGHTS VIDEOS
► ua-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 #cpp20
Переглядів: 181

Відео

C++ Insights - Episode 56: Understanding the magic behind C++17's structured bindings
Переглядів 69028 днів тому
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,1 тис.2 місяці тому
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!
Переглядів 6443 місяці тому
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
Переглядів 5223 місяці тому
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
Переглядів 5785 місяців тому
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,1 тис.5 місяців тому
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
Переглядів 7437 місяців тому
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,1 тис.8 місяців тому
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
Переглядів 7698 місяців тому
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
Переглядів 8059 місяців тому
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 тис.11 місяців тому
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
Переглядів 70311 місяців тому
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
Переглядів 680Рік тому
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,1 тис.Рік тому
In this episode, you learn why you cannot have a templated copy constructor. Source code: cppinsights.io/s/d72b062c 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...
🇩🇪 ESE Kongress 2020: C++ Insights: Sehen Sie Ihren Quellcode mit den Augen eines Compilers
Переглядів 140Рік тому
🇩🇪 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
Переглядів 2 тис.Рік тому
C Insights - Episode 42: C 20: When is a struct an aggregate
🇩🇪 ESE Kongress 2022: C++ 20 Templates - Die nächste Generation
Переглядів 190Рік тому
🇩🇪 ESE Kongress 2022: C 20 Templates - Die nächste Generation
C++ Insights - Episode 41: How type-traits work
Переглядів 3,6 тис.Рік тому
C Insights - Episode 41: How type-traits work
C++ Insights - Episode 40: The traps of returning references
Переглядів 1,1 тис.Рік тому
C Insights - Episode 40: The traps of returning references
C++ Insights - Episode 39: Recursion-free templates with C++17s fold expressions
Переглядів 1,3 тис.Рік тому
C Insights - Episode 39: Recursion-free templates with C 17s fold expressions
C++ Insights - Episode 38: User-provided vs. user-declared
Переглядів 999Рік тому
C Insights - Episode 38: User-provided vs. user-declared
C++ Insights - Episode 37: C++20's range-based for-loop with initializer
Переглядів 882Рік тому
C Insights - Episode 37: C 20's range-based for-loop with initializer
C++ Insights - Episode 36: Coroutine customization points
Переглядів 454Рік тому
C Insights - Episode 36: Coroutine customization points
C++ Insights - Episode 35: C++20's Coroutines: One thing you should watch out for
Переглядів 509Рік тому
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
Переглядів 263Рік тому
C Insights - Episode 34: C 20: Captureless lambdas in unevaluated contexts
C++ Insights - Episode 33: C++17's Class Template Argument Deduction
Переглядів 595Рік тому
C Insights - Episode 33: C 17's Class Template Argument Deduction
C++ Insights - Episode 32: Captureless lambdas with a static invoker
Переглядів 3382 роки тому
C Insights - Episode 32: Captureless lambdas with a static invoker
C++ Insights - Episode 31: Visualize padding with C++ Insights
Переглядів 9062 роки тому
C Insights - Episode 31: Visualize padding with C Insights
C++ Insights - Episode 30: C++17's constexpr if
Переглядів 7332 роки тому
C Insights - Episode 30: C 17's constexpr if

КОМЕНТАРІ

  • @sanjaygatne1424
    @sanjaygatne1424 14 годин тому

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

  • @almondengine
    @almondengine 17 днів тому

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

  • @EddingDefault
    @EddingDefault 25 днів тому

    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.

  • @mm1979dk
    @mm1979dk 28 днів тому

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

  • @mm1979dk
    @mm1979dk 28 днів тому

    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 23 дні тому

      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 28 днів тому

    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 28 днів тому

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

  • @McDonaldIbekwe
    @McDonaldIbekwe Місяць тому

    Nice video with very clear explanation.

  • @MarcosCpp
    @MarcosCpp Місяць тому

    I liked this video, quick and straight to the point

  • @KalkiCharcha-hd5un
    @KalkiCharcha-hd5un Місяць тому

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

    • @mm1979dk
      @mm1979dk 28 днів тому

      Try pinch to zoom👌

  • @xorbe2
    @xorbe2 2 місяці тому

    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 2 місяці тому

    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 2 місяці тому

      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 2 місяці тому

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

  • @tomkirbygreen
    @tomkirbygreen 2 місяці тому

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

    • @TheMR-777
      @TheMR-777 2 місяці тому

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

    • @andreas_fertig
      @andreas_fertig Місяць тому

      Thanks! I think its to late to change.

  • @valentinsiltsenko
    @valentinsiltsenko 2 місяці тому

    Thank you for the great explanation!

  • @Luckygangff
    @Luckygangff 2 місяці тому

    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 2 місяці тому

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

  • @rafalmichalski4893
    @rafalmichalski4893 2 місяці тому

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

  • @AlfredoCorrea
    @AlfredoCorrea 3 місяці тому

    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 3 місяці тому

    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 3 місяці тому

    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 4 місяці тому

    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 3 місяці тому

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

  • @adeelahmad9875
    @adeelahmad9875 4 місяці тому

    Nice !!

  • @panjak323
    @panjak323 5 місяців тому

    So why isn't noexcept the default?

    • @andreas_fertig
      @andreas_fertig 5 місяців тому

      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 5 місяців тому

    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 5 місяців тому

      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 5 місяців тому

      @@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 2 місяці тому

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

  • @anjumlatif3052
    @anjumlatif3052 5 місяців тому

    Video voice quality is not good.

  • @zeez7777
    @zeez7777 6 місяців тому

    NIce

  • @tomkirbygreen
    @tomkirbygreen 6 місяців тому

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

    • @andreas_fertig
      @andreas_fertig 5 місяців тому

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

  • @davithov
    @davithov 7 місяців тому

    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 5 місяців тому

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

    • @andreas_fertig
      @andreas_fertig 5 місяців тому

      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 5 місяців тому

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

  • @davithov
    @davithov 7 місяців тому

    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 7 місяців тому

      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 7 місяців тому

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

  • @PaulTopping1
    @PaulTopping1 8 місяців тому

    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 8 місяців тому

    Great

  • @phusicus_404
    @phusicus_404 8 місяців тому

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

  • @ЮрійГордієнко-х2з
    @ЮрійГордієнко-х2з 9 місяців тому

    Wrong

  • @JustABigFan2010
    @JustABigFan2010 9 місяців тому

    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 UA-cam videos.

    • @phusicus_404
      @phusicus_404 8 місяців тому

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

    • @JustABigFan2010
      @JustABigFan2010 8 місяців тому

      @@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 10 місяців тому

    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 9 місяців тому

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

  • @zuofu-ziv
    @zuofu-ziv 10 місяців тому

    binge watching the series

    • @andreas_fertig
      @andreas_fertig 9 місяців тому

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

  • @zuofu-ziv
    @zuofu-ziv 10 місяців тому

    very insightful!

  • @reflec8525
    @reflec8525 10 місяців тому

    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 9 місяців тому

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

  • @David_Friberg
    @David_Friberg 11 місяців тому

    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 11 місяців тому

    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 11 місяців тому

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

    • @VincentZalzal
      @VincentZalzal 11 місяців тому

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

    • @davidf7196
      @davidf7196 11 місяців тому

      @@VincentZalzal Seems UA-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 11 місяців тому

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

    • @VincentZalzal
      @VincentZalzal 11 місяців тому

      @@David_FribergFound it, thanks.

  • @stanbarvinsky1011
    @stanbarvinsky1011 11 місяців тому

    great tip! thank you

  • @TheBuilder
    @TheBuilder 11 місяців тому

    I'll think about it

  • @valentinsiltsenko
    @valentinsiltsenko 11 місяців тому

    Great tip. Thanks a lot!

  • @ggladilov
    @ggladilov 11 місяців тому

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

    • @andreas_fertig
      @andreas_fertig 11 місяців тому

      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 11 місяців тому

      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 11 місяців тому

      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.

  • @mohammadghasemi2402
    @mohammadghasemi2402 Рік тому

    In the last example, why the compiler needs to increment and decrement acicp?

    • @andreas_fertig
      @andreas_fertig 11 місяців тому

      I'm not sure I understand your question.

    • @mohammadghasemi2402
      @mohammadghasemi2402 11 місяців тому

      @@andreas_fertig In the video you said the reason why the compiler does not deduce the const after the pointer is that it wants to increment/decrement it. My question is why the compiler wants to increment/decrement acicp at all? Because that is what necessitates the removal of the constness from the pointer.

    • @andreas_fertig
      @andreas_fertig 11 місяців тому

      ​@@mohammadghasemi2402 Sorry, that was badly phrased. The compiler cannot ignore the fact that the pointer points to constant memory. We also made the pointer itself const, by adding a top-level qualifier. Hence we cannot increment or decrement the pointer. That top-level qualifier is not part of the type. That's what the compiler ignores.

  • @PaulTopping1
    @PaulTopping1 Рік тому

    Interesting edge case! But is the programmer that writes such code really trying to define a copy ctor? I suspect not. IMHO, a better way of describing this is that a template function that accidentally matches the class's copy ctor hides it. If one adds an explicit copy ctor to your example, it is called for the Apple f2{f0}; line.

    • @SteelBlueVision
      @SteelBlueVision Рік тому

      I think you missed the point, it seems to be still generating and using the default copy constructor and f2 is just a copy of f0 via copy initialization.

  • @rinket7779
    @rinket7779 Рік тому

    Is this only cos the copy ctor is still implicitly defined? What if I defined a SMF which would undefine the implicit copy ctor? Would it then use the template copy ctor? EDIT: BY using a Apple(T&&) generic template constructor I got it to be invoked for both copies and move construction.