Does accessing the first field of a struct via a C cast violate strict aliasing?

First, it is legal to cast in C. § Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, … Read more

Are non dereferenced iterators past the “one past-the-end” iterator of an array undefined behavior?

Yes, your program has undefined behaviour if you form such a pointer. That’s because the only way you can do so is to increment a valid pointer past the bounds of the object it points inside, and that is an undefined operation. [C++14: 5.7/5]: When an expression that has integral type is added to or … Read more

What does the void() in decltype(void()) mean exactly?

Using a hyperlinked C++ grammar, the parsing of decltype(void()) is: decltype( expression ) decltype( assignment-expression ) decltype( conditional-expression ) … lots of steps involving order of operations go here … decltype( postfix-expression ) decltype( simple-type-specifier ( expression-listopt ) ) decltype( void() ) So void() is a kind of expression here, in particular a postfix-expression. Specifically, … Read more

Alternative for-loop syntax [duplicate]

Unfortunately, this is not easy to read. You are misreading the second case of the for statement. The first semicolon is an integral part of declaration and thus hidden to your eyes. You can easily check such syntax questions by looking into Annex A. There you have: (6.7) declaration: declaration-specifiers init-declarator-listopt ; static_assert-declaration

Is it defined behavior to reference an early member from a later member expression during aggregate initialization?

Your second case is undefined behavior, you are no longer using aggregate initialization, it is still list initialization but in this case you have a user defined constructor which is being called. In order to pass the second argument to your constructor it needs to evaluate foo.i but it is not initialized yet since you … Read more

Why is the std::initializer_list constructor preferred when using a braced initializer list?

§ [over.match.list]/p1: When objects of non-aggregate class type T are list-initialized (8.5.4), overload resolution selects the constructor in two phases: Initially, the candidate functions are the initializer-list constructors (8.5.4) of the class T and the argument list consists of the initializer list as a single argument. If no viable initializer-list constructor is found, overload resolution … Read more