C++ Weekly - Ep 190 - The Important Parts of C++17 in 10 Minutes

Поділитися
Вставка
  • Опубліковано 28 лип 2024
  • ☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
    Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
    ► ndctechtown.com/workshops/c-b...
    Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024
    ► cppunderthesea.nl/workshops/
    T-SHIRTS AVAILABLE!
    ► The best C++ T-Shirts anywhere! my-store-d16a2f.creator-sprin...
    WANT MORE JASON?
    ► My Training Classes: emptycrate.com/training.html
    ► Follow me on twitter: / lefticus
    SUPPORT THE CHANNEL
    ► Patreon: / lefticus
    ► Github Sponsors: github.com/sponsors/lefticus
    ► Paypal Donation: www.paypal.com/donate/?hosted...
    GET INVOLVED
    ► Video Idea List: github.com/lefticus/cpp_weekl...
    JASON'S BOOKS
    ► C++23 Best Practices
    Leanpub Ebook: leanpub.com/cpp23_best_practi...
    ► 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/objectlifetimepuz...
    ► Object Lifetime Puzzlers Book 2
    Amazon Paperback: amzn.to/3whdUDU
    Leanpub Ebook: leanpub.com/objectlifetimepuz...
    ► Object Lifetime Puzzlers Book 3
    Leanpub Ebook: leanpub.com/objectlifetimepuz...
    ► Copy and Reference Puzzlers Book 1
    Amazon Paperback: amzn.to/3g7ZVb9
    Leanpub Ebook: leanpub.com/copyandreferencep...
    ► Copy and Reference Puzzlers Book 2
    Amazon Paperback: amzn.to/3X1LOIx
    Leanpub Ebook: leanpub.com/copyandreferencep...
    ► Copy and Reference Puzzlers Book 3
    Leanpub Ebook: leanpub.com/copyandreferencep...
    ► 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...
    ► C++ Best Practices Forkable Coding Standards - github.com/cpp-best-practices...
    O'Reilly VIDEOS
    ► Inheritance and Polymorphism in C++ - www.oreilly.com/library/view/...
    ► Learning C++ Best Practices - www.oreilly.com/library/view/...
  • Наука та технологія

КОМЕНТАРІ • 64

  • @ankitthakar2671
    @ankitthakar2671 4 роки тому +54

    There are some more important features as well
    1. Binary literals
    2. if constexpr
    3. Nested namespace

    • @donaldherman1741
      @donaldherman1741 4 роки тому +4

      Isn't binary c++ 14?

    • @Embedonix
      @Embedonix 4 роки тому +4

      I second the Nested namespace thing... very handy!!!!!

    • @ankitthakar2671
      @ankitthakar2671 4 роки тому +2

      @@donaldherman1741 :- Yes, You're right my mistake.

    • @vorpal22
      @vorpal22 4 роки тому

      @@Embedonix Agree with this. Having to manually nest namespaces is a PITA.

    • @MrAlbinopapa
      @MrAlbinopapa 4 роки тому +4

      I don't use binary literals all that often, but if constexpr is a time/code saver. Being able to write one function and just have it switch on a type for example ( if constexpr( std::is_integral_v ) else if constexpr( std::is_floating_point_v )...
      Makes life grand.
      The nested namespace is also very nice, but I think it can be cleaner to have actual nested namespaces in some cases, but I'm glad the feature is there.

  • @YSoreil
    @YSoreil 4 роки тому +21

    I just now realize how many C++ 17 features I already took for granted. Structured bindings and if with initializer are big ones. I use the modern if all the time. I wrote a lot of Go before C++ 17 so I was very happy to see C++ finally adopt that feature. Stringview is the one thing I have never used so far I'll have to try it out some day.

  • @LesleyLai
    @LesleyLai 4 роки тому +5

    I love the features that makes meta programming more "normal." Fold expression belongs to that category.

    • @LesleyLai
      @LesleyLai 4 роки тому

      If a meta-programming hack is widely used enough, then a language feature should replace that.

  • @antoniocs8873
    @antoniocs8873 4 роки тому +6

    Another great episode! Every time I see the notification for C++ weekly I am "Guaranateed" to learn something.

  • @NoNameAtAll2
    @NoNameAtAll2 4 роки тому +11

    Just want to remind that "inline static" for static members happened in c++17 also
    No more need to create nice class only to add some additions outside of it just for sake of it

    • @frydac
      @frydac 4 роки тому +5

      yes! especially in a non-template header only small class, for which you then have to make a translation unit to initialize the static member.

  • @thestarinthesky_
    @thestarinthesky_ 11 місяців тому +1

    Awesome! Thank you Jason. ❤

  • @Embedonix
    @Embedonix 4 роки тому +27

    so far, 3 people have confirmed they are using c++98

    • @tcc1234
      @tcc1234 3 роки тому +2

      4 people by 2021...

  • @undercrackers56
    @undercrackers56 Рік тому +1

    I used to work with C++ quite a lot in the 90's. For the last 15 years I have worked mostly with C and Java. I am now looking at C++ again for a big project and thinking that the people governing the language have taken a difficult language, made it more difficult and then wrapped it in impenetrable nomenclature for good measure. So, back to C I guess.

  • @coreywn
    @coreywn 4 роки тому +2

    Structured bindings! I didn’t know that was a thing!!

  • @oliviercloud5393
    @oliviercloud5393 2 роки тому

    Awesome content !!

  • @AxelStrem
    @AxelStrem 4 роки тому +2

    at 0:40 subtitles say "The number one thing that I want to mention is guaranteed COPULATION"... I knew C++ was the right choice for me

    • @NomoregoodnamesD8
      @NomoregoodnamesD8 4 роки тому

      the very chaotic template function adds things then returns them from Maine

  • @yackawaytube
    @yackawaytube 4 роки тому +1

    Every language has added structural binding.... welcome to the club C++17

  • @timhaines3877
    @timhaines3877 4 роки тому +19

    I'm surprised `if constexpr` didn't make the list, given its ability to, in some cases, completely remove SFINAE overhead. I really like if-init because it reduces scope, but I really hate it because no matter how you format it, its presence really interrupts the flow of reading code.

  • @codyheiner3636
    @codyheiner3636 4 роки тому +4

    Is there a way to, or has there been discussion around structured bindings without hiding the types? Some way to write [ int x, int y ] = my_point;

  • @gast128
    @gast128 4 роки тому +2

    Nice additions but for me the clear winner is the parallel STL. In general C++ is still lacking in libraries which is a weak spot compared to Java and .NET when you are developing applications.

  • @DrMcCoy
    @DrMcCoy 4 роки тому +1

    I just wish structured bindings had a way to tell it that one element isn't used, i.e. to silence the -Wunused* warnings

  • @Mr4NiceOne
    @Mr4NiceOne 4 роки тому +12

    C++ 17 feature I use the most is:
    auto & f = foos.emplace_back();

    • @frydac
      @frydac 4 роки тому +2

      yup so much cleaner,
      std::map::try_emplace(key, ctor_args) is also a beauty imho, which otherwise is a few lines to express: if this key exists, give me the element, if it doesn't yet, create it and give it to me. Especially when initializing a map in a loop, this is much cleaner and so intuitive. For the c++14 code I still have to work on I created a free function that does something similar (I never identified this as something to extract until I saw this member function.. shame on me!)

    • @OmarChida
      @OmarChida 3 роки тому

      Had that in my own standard library that I wrote 3 years ago

    • @Mr4NiceOne
      @Mr4NiceOne 3 роки тому

      ​@@OmarChida Oh god, your comment reminded me that I used to watch Jason, and couple other guys, while learning modern c++.
      I wasted so much time trying to learn it instead of actually doing interesting shit; I wouldn't touch that shit again with a mile long insulated stick now.

    • @OmarChida
      @OmarChida 3 роки тому +1

      @@Mr4NiceOne I myself was an Ultra-Orthodox C++ programmer been using C++11 for years now. It's just since couple of months that I started really modernizing my knowledge since I saw some useful features in the new standards.

  • @cgerman
    @cgerman 4 роки тому +3

    I think you forgot constexpr if

  • @Slayeah
    @Slayeah 4 роки тому

    Also the flag "-fno-elide-constructors" does not work anymore, right? It is now guaranteed.

  • @simonhrabec9973
    @simonhrabec9973 3 роки тому +2

    For std::array is it possible to only deduce the size and not the type?

    • @fob3476
      @fob3476 2 роки тому

      Unfortunately not

  • @RichardBaileyrichoncode
    @RichardBaileyrichoncode 3 роки тому

    What is the scratchpad tool? Love seeing the asm at the right to verify what is happening.

    • @OGBhyve
      @OGBhyve 3 роки тому +1

      compiler explorer godbolt.org

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

    Hey Jason, trying to find an entry point for C++ from being a designer for years. I like your teahing style but cant find an entry point. Even with this video im not sure many of the terms you're referring to. Do you have any videos that are even more basic as a starting point?

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

      My main recommendation is A Tour of C++ for a high level overview of the language. Unfortunately I don't have the channel organized very well for beginners, but it is on my to-do list to make a playlist for that purpose.

  • @GabeRundlett
    @GabeRundlett 4 роки тому

    At the end of the video he says when he writes 'void func()' we're accessing the global scope, don't write code like this. What is he referring to here and why shouldn't it be done?

    • @frydac
      @frydac 4 роки тому

      He says: "we are accessing the global(ly defined) values", which is basically the same as you said of course.
      In this case, the function could be better if it got a reference to 'a' values pair via its function arguments, and for example define the pair in the main function in stead of the global scope, and pass it to func(values) when you call it. The advantage would be to decouple the function func from the global variable instance, so it could be (re)used in other contexts, for example it is then much easier to test in a unittest: unittests typically reinitialize all variables for each testcase, but with global state, this state is defined/initialized once for the unittest executable and then reused for each testcase/section/.., so you'd have to reinitialize/clear it explicitly in each testcase, which is usually error prone, and generally it is a good principal in unittests to keep each testcase completely independent, as the order of testcase execution could otherwise have influence on the outcome of the testcase.

    • @GabeRundlett
      @GabeRundlett 4 роки тому

      @@frydac To clarify, you're saying that he is saying something like the example below would be better code, because values is allocated inside the main function's stack frame, and that it would be able to be used in any instance, using any pair given to the function? Are there performance or other related issues with global state, or is it just that the memory is not freed by the operating system until the program ends?
      #include
      void func(const std::pair &pair) {
      if (auto [first, second] = pair; first > 5) {
      // Do important work
      }
      }
      int main() {
      std::pair values{1, 2};
      func(values);
      }

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

    8:40, would C++17 accept *std::pair values {1,2}* , deducing the template args?

  • @perfectionbox
    @perfectionbox 3 роки тому

    c++ veteran: here's the new code, totally idiomatic with latest features
    newbie: wtf

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

    guarana-teed 🍒💪😤

  • @romanemul1
    @romanemul1 4 роки тому

    Excuse me ? What is a widget?

    • @Embedonix
      @Embedonix 4 роки тому

      just a "thing"

    • @frydac
      @frydac 4 роки тому +2

      in many graphical user interface libaries a basic building block is called a 'widget', the gui is then composed of a hierarchy of widgets representing the different 'drawable' elements of the gui. To implement different concrete gui elements, one would derive from such a widget and implement specific draw/resize/... functions. Then to decouple the exact type of the widget, e.g. Button, MyInputForm.. from the code managing the hierarchy, a factory can be used that will construct the concrete type internally but returns a (owning) pointer to the base widget type to compose this hierarchy with, and use it for e.g. drawing/resizing. In this way when the implementation of the specific widget is changed, this code would usually not even have to be recompiled.
      Imho, this is why he used it as a typical example of a factory function creating something and returning that something which also passes ownership to the caller of the function, and for which you can now be sure no copy nor move is happening under the hood.
      This has been an optimization in C++ compilers for longer that 20years, and maybe much longer (I recently read about it in 'Inside the C++ object model' from '96, very good book btw!), but this was not guaranteed, in certain circumstances the optimization would not happen (e.g. multiple return statements in the factory function) so you'd have to know your compiler's implementation/rules to know for sure it was elided.

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

    I m really i want to bay your book 📖
    Your are very awesome son
    Is there ebook.

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

      leanpub.com/cppbestpractices

  • @DMStern
    @DMStern 4 роки тому +5

    Why are we even forced to write "constexpr" everywhere anymore? Wouldn't it make more sense to just make it the default, and add some uglification if for some odd reason there's something that must not be evaluated at compile time? In a few years' time I guarantee that this will be thought of as one of the ugliest warts of the language.

    • @vorrnth8734
      @vorrnth8734 4 роки тому +1

      DMStern It could break existing code.

    • @konstantinudovickij
      @konstantinudovickij 4 роки тому

      What Vornth said. No need to turn C++ into Python (version wise).

    • @DMStern
      @DMStern 4 роки тому +4

      @@vorrnth8734 The compiler has always been allowed to evaluate constant expressions at compile time. C++11 constexpr just added a set of constraints that gave compilers a minimum set they had to be able to handle. Every standard version since has loosened the constraints further, and we're now almost at the stage where the whole language can be used. But if you follow the "constexpr everything" mantra in your code, then it becomes completely redundant, just adding more visual noise.

    • @raymundhofmann7661
      @raymundhofmann7661 4 роки тому +1

      If it can be evaluated at compile time, the compiler will do it often anyway, it is called optimization.

    • @cH3rtzb3rg
      @cH3rtzb3rg 4 роки тому

      @@vorrnth8734 The compiler needs to check anyway if it actually can evaluate a constexpr function at compile-time -- and it is legal to mark functions constexpr, which never will actually be constexpr (e.g., godbolt.org/z/lLjXNF), so the standard could have said as well, something like "If you assign something to a constexpr variable, let the compiler figure out, if it can evaluate it at compile-time". Of course "what the compiler can evaluate at compile time" needs to be specified rigorously (I did not fully think this through, I guess this could still break something, maybe some SFINAE constructs will break).
      I do fully agree that not breaking existing code is a very important feature of C++

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

    std:any

  • @01MeuCanal
    @01MeuCanal 4 роки тому

    Your videos are very cool but I'm kinda novice in C++ so I would like your explanations could consider that many viewers are not advanced, because if someone is advanced he may not need tutorials right?