C++ Features You Might Not Know - Jonathan Müller - C++ on Sea 2023

51,441
0
Published 2023-09-08
cpponsea.uk/
---

C++ Features You Might Not Know - Jonathan Müller - C++ on Sea 2023

C++ is a big language — the upcoming C++23 standard will be over 2000 pages long. This talk will cover some obscure features you might not know. We will cover strange syntax like commutative array indexing and complicated declarators, surprising cases of undefined behavior in frequently used operators contrasted with a surprising lack of undefined behavior in operations that really shouldn't work, overlooked language facilities — some of them actually useful, and half-forgotten standard library functions — some of them for good reason.

For each feature, we will talk about the what, the why, and how you can use it to write better (or much, much worse) C++ programs.
---

Slides: github.com/philsquared/cpponsea-slides/tree/master…
Sponsored by think-cell: www.think-cell.com/en/
---

Jonathan Müller

Jonathan is a library developer at think-cell. In his spare time, he works on various C++ open source libraries for memory allocation, cache-friendly containers, or parsing. He also blogs at foonathan.net and is a member of the C++ standardization committee.
---

C++ on Sea is an annual C++ and coding conference, in Folkestone, in the UK.
- Annual C++ on Sea, C++ conference: cpponsea.uk/
- 2023 Program: cpponsea.uk/2023/schedule/
- Twitter: twitter.com/cpponsea
---

YouTube Videos Filmed, Edited & Optimised by Digital Medium: events.digital-medium.co.uk/

#cpp #cpponsea #cppprogramming

All Comments (21)
  • @0xybelis
    I use + for char for streams. char c = 65; std::cout << c; // prints A std::cout << +c; // prints 65
  • @legofan2284
    The reason nobody uses valarray is nobody knows of its existence
  • @pmcgee003
    The man is a farmer. Outstanding in his field. 👍
  • @Bolpat
    4:35 C++ making it’s debut in 1998. 22 years later, technology has advanced so much, we found out that inequality means not being equal.
  • @acestapp1884
    Lol I got called out in a code review last week for an 'else for'. Rounding to even is also called Bankers' Rounding, and is a tiny bit more stable because half of the .5s are rounded down and half are rounded up.
  • @failgun
    These sorts of talks are always my favourite. Cursed code, great humour, but still teach deep (maybe even useful) things about the language
  • @N....
    Great talk, I learned some interesting tidbits. I'm surprised I didn't know about the dynamic_cast feature, that's pretty nifty.
  • @XDzZyq
    it seems sizeof(+a)["12345"] == '1' but (sizeof(+a))["12345"] == '4'. Built on MSVC
  • Another use case of dynamic_cast is when you overload delete operator. Because you need to pass the exact same address to the free that was returned from malloc in your overloaded new operator.
  • @vsarcawastaken
    At 4:00, the comma operator is very useful when defining macros, since it allows you to run multiple statements in the place of one.
  • @oriyadid
    Great talk, very entertaining!
  • @Hauketal
    About those negative % operations: Ada has both rem and mod operators, so one can choose. But not compatible with C heritage.
  • @oisyn-
    Re 4:20 and 7:40. The (overloaded) comma operator can be useful to deal with function returns in template code where the return type might be void. Because void is an incomplete type, you can't assign it to a local variable. However, void is allowed as an operand for the comma operator. You can't overload the comma where one of the operands is void, but we can use that to our advantage. This allows you to do something like: template<class T> struct wrapped { T value; }; template<> struct wrapped<void> { }; template<class T> T unwrap(wrapped<T> t) { return t.value; } void unwrap(wrapped<void>) { } // the trick: template<class T> wrapped<T> operator,(T t, wrapped<void>) { return { t }; } template<class T> auto foo(T t) { // call some unknown overloaded function that might return void // auto r = bar(t); // This won't work if bar(t) returns void auto r = (bar(t), wrapped<void>()); // But this will // do something else, otherwise we could've just done 'return bar(t)' return unwrap(r); // return the original value, or void } When bar(t) is not void, the template operator,() is invoked, returning the result of bar(t) wrapped in a wrapped<U> as per its implementation. If bar(t) is void, you get the built-in operator,(), which returns the second operand, a wrapped<void> in this case. We can then copy this around safely, do some other stuff, and then finally unwrap the original value. Unwrapping a wrapped<void> just returns void, and you are in fact allowed to return the result of an expression of type void in a function that returns void. This code was just to get the idea across btw, it can use some perfect forwarding love. Of course there are other ways to solve this problem (e.g., constexpr if or specializations), but they usually involve code duplication.
  • @maxoumimaro
    I love that this guy taught me so much about c++ and that I could probably teach him kinda of the same thing on template and compile time c++ xD
  • @rimaraf999
    Excellent talk! I definitely learned a thing or two.
  • @JohnDlugosz
    re valarray: Back in the day, like right after C++98 was published, the word was that valarray was a goofup and we should just ignore it. There was no streaming videos, but there were talks given with people listening, just like they do today to go over all the new features when an updated standard is published. The insight from those talks were published in the major programming magazines as this was just before the ubiquity of Internet access and the demise of magazines, and also a few blog posts. I also don't remember why . Perhaps it was fixed at some point, e.g. C++11? I have some vague memory that this might be the case. Browsing a bit, I'm reminded that it proved inferior to 3rd part libraries that used expression templates .
  • @kippers12isOG
    The rounding to nearest even number is to avoid errors growing too much