Non-conforming C++: the Secrets the Committee Is Hiding From You - Miro Knejp - CppCon 2019
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about undefined behavior in array handling, placement new, pointer arithmetic and non-standard GCC extensions like the ?: operator.
Recommended by 4 people across 4 different months, 2019 to 2023.
What it is
A CppCon 2019 talk by Miro Knejp on compiler extensions and non-standard C++ features such as statement expressions, computed goto, the Elvis operator and flexible array members. It also covers the standard's gaps around arrays, placement new and pointer arithmetic over adjacent objects.
What people said
Recommends GeckoGeorge · 2019-10 · Stack Overflow · on “Accessing struct data members via pointer arithmetic”
There's a cppcon talk that mentions this! [this video] So yes, it's undefined behaviour, because classes and arrays don't share a common initial sequence. Edit: Miro Knejp introduces that slide at around 3:44 if you want more context for all the non-c++ on the slide, but the question and answer is really the only part of the talk that goes into your question.
Recommends Matthijs · 2019-10 · Stack Overflow · on “Can placement new for arrays be used in a portable way?”
With regard to treating a collection of adjacent objects individually constructed in contiguous memory as an array object, see C++ Core Issue 2182 [LINK] . Unfortunately this doesn't really seem to have been resolved yet, which means there might not be any way to in-place construct an array object that's guaranteed to work by the standard without any undefined behaviour. This problem is also analyzed in this video [this video] .
Recommends NathanOliver · 2019-12 · Stack Overflow · on “Constructing array of objects in uninitialized memory”
There is a good talk at cppcon2019 that goes over just this. The start of that section is at: [this video] Basically, you are SOL since you don't have an array you can't do pointer arithmetic.
Recommends Marek R · 2023-08 · Stack Overflow · on “How to convert c++ to c#?”
Offtopic: this macro looks bad - since it can be easily replaced by function or function template. Additionally it looks like some hack for variable length 2d array, which is in this form definitely UB. See this cppcon [this video] .
Recommends Marek R · 2023-09 · Stack Overflow · on “Switch statement vs seperate function calls for performance”
While I think that version 2 looks 'cleaner' I am a bit worried that the switch statement in second option would affect performance considering this is a vm and this code can run very frequently. So my question is - which option is better and would the switch statement in option 2 actually affect performance or would the compiler maybe optimise it and split it into two functions by itself? There is no simple proper answer for that. Remember that C++ compilers are using "as-if rule" [LINK] , as a result such simple tweak you are proposing can be completely transparent for final machine code (compiler may decide to inline all your small functions). Predicting what compiler can optimize, how ha
Recommends Marek R · 2023-09 · Stack Overflow · on “Switch statement vs seperate function calls for performance”
If you do not do measurements you are not doing performance improvements, but just do guessing. This is depended on so many factors that any claim may look logical, but be completely wrong. Only performance measurements has a final word. Please see this cppcon where similar issue is addressed [this video] .
Cites lornova · 2019-10 · Stack Overflow · on “How to enable the C/C++ "Conditional with Omitted Operand" (aka Elvis Operator "?:") in GCC”
In the talk Non-conforming C++ [this video] from CppCon2019 it is introduced the " Elvis Operator [LINK] " in C++, which is a non-standard extension supported by many compilers. It works by omitting the middle operand of an ?: expression: std::shared_ptr<foo> read(); std::shared_ptr<foo> default_value(); auto read_or_default() { return read() ?: default_value(); } This is exactly the sample taken from slide 11 of the presentation [LINK] . However when I build it on GCC 7.4.0 on Ubuntu 18.04, or with GCC 8.2.0 on MinGW, using the -std=gnu++14 flag, I get this error: error: lvalue required as unary '&' operand return read() ?: default_value(); ^ In the talk it is said that this extension is present
Cites Ad N · 2019-10 · Stack Overflow · on “Array placement-new requires unspecified overhead in the buffer?”
Wow, ended up here watching [this video] This is extremely scary, and it ensues that placement new for array is practically un-usable. Are there warnings in place on majors compilers to avoid such catastrophic failure?
Cites name · 2021-08 · Stack Overflow · on “Is it undefined behaviour to access two objects of type T declared next to each other using T[]?”
I recently watched a CppCon talk by Miro Kenjp on "Non-conforming C++: the Secrets the Committee Is Hiding From You". At 28:30 ( [this video] ) He states that accessing doubles next to each other was UB and I don't quite understand why. Can someone explain why is this the case? And if this is the case then surely its UB to access other things this way for example: int* twoInts = malloc(sizeof(int) * 2); int secondInt = twoInts[1]; //Undefined behaviour?
Cites Marko Grdinić · 2021-08 · Stack Overflow · on “Is there a heap allocated fixed-size array in the standard library?”
I am looking for the equivalent of .NET fixed size arrays in C++ and am having real trouble finding this information by Googling it. std::array imitates Pascal by having the length embedded directly in its type. std::vector is good, but passes around 3 pointers on the stack where I'd prefer just one. Also rather than iterator based arrays, since I am working on a C++ backend for a functional language [LINK] I'd prefer it to be size based so as too match the arrays in the .NET and the Cython backends. Should I implement my own array class instead? Edit : What I am asking for is a flexible array (such as this one [this video] ) as a part of a library.