Optimizing away a “while(1);” in C++0x

To me, the relevant justification is: This is intended to allow compiler transfor- mations, such as removal of empty loops, even when termination cannot be proven. Presumably, this is because proving termination mechanically is difficult, and the inability to prove termination hampers compilers which could otherwise make useful transformations, such as moving nondependent operations from … Read more

Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not?

Yes, it’s legal. From the C99 draft standard: §6.5.2.1, paragraph 2: A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to … Read more

A positive lambda: ‘+[]{}’ – What sorcery is this? [duplicate]

Yes, the code is standard conforming. The + triggers a conversion to a plain old function pointer for the lambda. What happens is this: The compiler sees the first lambda ([]{}) and generates a closure object according to §5.1.2. As the lambda is a non-capturing lambda, the following applies: 5.1.2 Lambda expressions [expr.prim.lambda] 6 The … Read more

Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++14?

Yes, this change was driven by changes in the language which makes it undefined behavior if an indeterminate value is produced by an evaluation but with some exceptions for unsigned narrow characters. Defect report 1787 whose proposed text can be found in N39141 was recently accepted in 2014 and is incorporated in the latest working … Read more

When does invoking a member function on a null instance result in undefined behavior?

Both (a) and (b) result in undefined behavior. It’s always undefined behavior to call a member function through a null pointer. If the function is static, it’s technically undefined as well, but there’s some dispute. The first thing to understand is why it’s undefined behavior to dereference a null pointer. In C++03, there’s actually a … Read more