Auto Auto Auto
Shafik Yaghmour's Blog
by
8M ago
Auto Auto Auto In my C++ quiz #237 I asked the following question #237 Given the following in C++: struct A{}; using T = auto() -> auto(*)() -> auto(*)() -> A; // Without checking, is this well-formed? The short answer is, Yes. The likely follow-up question is, what does T represent? Peeling the Onion Often when we have a complicated declaration, it is helpful to break it down into smaller pieces and build up to the final declaration. Kind of like peeling back the layers of an onion or in this case adding a layer one at a time. In that vain we can start with the following declara ..read more
Visit website
Let's enumerate the UB
Shafik Yaghmour's Blog
by
10M ago
Let’s enumerate the UB Some time ago I started working on P1705 Enumerating Core Undefined Behavior, I have collected a large set of undefined behavior (UB) during that time. There is going to be a lot of work involved in getting the annex into shape and editing it into the standard. While this work is ongoing, I will take some time to write blog posts to explore the set of undefined behaviors. In these posts I will cover: why we have each undefined behavior, some consequences of running afoul of each UB and tools we can use to catch UB. My plan is to enumerate them in the order they appear in ..read more
Visit website
Exploring Clangs Enum implementation and How we Catch Undefined Behavior
Shafik Yaghmour's Blog
by
1y ago
Intro In Part 1: Enums In C++, Choice is Oft Beguiled I talked about how enums work in C++ and the various choices we have. If you are not familiar with how enums work reading part 1 will help you to understand part 2. In this part we will dive into some of the clang internals for tracking the range of values of an enum and how it is used by UBSan and the optimizer. Finally we will look at constant expressions and a recent set of patches I landed to diagnose undefined behavior. Keeping Track of Positive and Negative Bits In clang for an enum without a fixed underlying type we need to track the ..read more
Visit website
Enums In C++, Choice is Oft Beguiled
Shafik Yaghmour's Blog
by
1y ago
The Basics In C++ we have several different options for enum types. These different choices affect the scope the enumerators are available in, the range of underlying values we can use and even whether setting certain values to an enum invokes undefined behavior. We are going to explore the different choices available and the consequences of those choices. I will follow this post with a second one exploring some of clang’s implementation details, undefined behavior, UBSan, and constant expressions. In C++11 forward we have three different types of enums. We have C-style plain enum, enum with e ..read more
Visit website
C++ initialization, arrays and lambdas oh my!
Shafik Yaghmour's Blog
by
1y ago
I recently ran a C++ weekly quiz #198 with the following code: int main() { int arr[5]{1,2}; return arr[ [](){return 4;}() ]; // What does main return? } Likely folks will be surprised that this does not compile (see it live). The standard says that two consecutive [ shall only be used to introduce an attribute, see [dcl.attr.grammar]p7: Two consecutive left square bracket tokens shall appear only when introducing an attribute-specifier or within the balanced-token-seq of an attribute-argument-clause. Originally when attributes was introduced into C++ this restriction did not exi ..read more
Visit website
The Usual Arithmetic Confusions
Shafik Yaghmour's Blog
by
2y ago
There are a lot of aspects of C++ that are not well understood and lead to all sorts of confusion. The usual arithmetic conversions and the integral promotions are two such aspects. Certain binary operators (arithmetic, relational and spaceship) require their operands to have a common type. The usual arithmetic conversions are the set of steps that gets operands to a common type. While the integral promotions brings integral types smaller than int and unsigned int to either int or unsigned int depending on which one can represent all the values of the source type. This is one of the areas in C ..read more
Visit website
CppCon 2021 Trip Report
Shafik Yaghmour's Blog
by
2y ago
CppCon 2021 was a hybrid format this year due to Covid-19 still not being done with us. Folks were allowed on site with proof of vaccination but folks were also allowed to attend completely remote. Last year was fully online and that was a pretty positive experience for me. This year I still wasn’t sure what to expect since I was attending remotely because it was a hybrid format. The schedule was shifted earlier US time-wise to allow those in Europe to still participate live. It also meant that some content would not be broadcast the same day but the next day and as I learned later some conten ..read more
Visit website
Down with typename
Shafik Yaghmour's Blog
by
3y ago
In C++ quiz quiz #117 I asked in C++20 where was typename required in code below: template<class T> struct S { typename/*A*/ T::R f(typename/*B*/ T::P p) { typename/*C*/ T::x *i; return {}; } }; and the answer is that it is required in position C. Previous to C++20 it would have been required in all three places but the proposal Down with typename! changed that. Previously as noted in the proposal: If X<T>::Y — where T is a template parameter — is to denote a type, it must be preceded by the keyword typename; otherwise, it is assumed to denote a name producing an ..read more
Visit website
Is Zero a Butterfly?
Shafik Yaghmour's Blog
by
3y ago
Is Zero a Butterfly? Wait? What? That sounds like a pretty odd question to ask for C++ but let’s explore a little and see where it takes us. So what is zero in C++? Perhaps the most obvious answer would be it is an integer and while this is true it misses a bigger story about zeros super powers in C++. What is super about zero? Well first some fun trivia. Zero is an octal literal, see the grammar for integer literals: octal-literal: 0 octal-literal 'opt octal-digit It is the only single digit octal literal, it won’t help you do anything special but might impress some of your friends. Ze ..read more
Visit website
Maximal Munch and C++
Shafik Yaghmour's Blog
by
3y ago
In C++ Weekly Quiz #106 we had the following set of declarations: template <int> using A = int; int x,y,*z; template<class T> class S; class C; and we were asked given these further declaration or expressions which of the following where well-formed: A. void f(A<0>=0); B. x=y/*z; C. S<::C>* cls; D. x+++++y; What all of these had in common is that they were ill-formed or once were ill-formed due to maximal munch. What is maximal munch? Maximal munch is a principle used during lexical analysis that says that and I will quote [lex.pptoken]p3.3: the next preprocessin ..read more
Visit website

Follow Shafik Yaghmour's Blog on FeedSpot

Continue with Google
Continue with Apple
OR