language-lawyer
Do I need to cast to unsigned char before calling toupper(), tolower(), et al.?
Yes, the argument to toupper needs to be converted to unsigned char to avoid the risk of undefined behavior. The types char, signed char, and unsigned char are three distinct types. char has the same range and representation as either signed char or unsigned char. (Plain char is very commonly signed and able to represent … Read more
trap representation
A trap representation is a catch-all term used by C99 (IIRC not by C89) to describe bit patterns that fit into the space occupied by a type, but trigger undefined behavior if used as a value of that type. The definition is in section 6.2.6.1p5 (with tentacles into all of 6.2.6) and I’m not going … Read more
Does initialization entail lvalue-to-rvalue conversion? Is `int x = x;` UB?
UPDATE: Following the discussion in the comments, I added some more evidence at the end of this answer. Disclaimer: I admit this answer is rather speculative. The current formulation of the C++11 Standard, on the other hand, does not seem to allow for a more formal answer. In the context of this Q&A, it has … Read more
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
Is it spread “syntax” or the spread “operator”?
It’s not an operator. In all senses of the word, it’s not one. It has been a huge misconception since it was introduced and despite popular opinion — it’s not one, and there are a few objective points to be made: It doesn’t fit the definition of an operator It can’t be used as an … Read more
Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not?
It is legal. According to the gcc documentation for C++, &array[5] is legal. In both C++ and in C you may safely address the element one past the end of an array – you will get a valid pointer. So &array[5] as an expression is legal. However, it is still undefined behavior to attempt 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
Order of evaluation of arguments using std::cout
The answer to this question changed in C++17. Evaluation of overloaded operators are now sequenced in the same way as for built-in operators (C++17 [over.match.oper]/2). Furthermore, the <<, >> and subscripting operators now have the left operand sequenced before the right, and the postfix-expression of a function call is sequenced before evaluation of the arguments. … Read more
What are the valid signatures for C’s main() function?
The C11 standard explicitly mentions these two: int main(void); int main(int argc, char* argv[]); although it does mention the phrase “or equivalent” with the following footnote: Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on. In addition, … Read more