Questions tagged [language-lawyer]

For questions about the intricacies of formal or authoritative specifications of programming languages and environments.

Typical questions concern gaps between "what will usually work in practice" and "what the spec actually guarantees", but problems with understanding the structure of the spec are also on topic.

Use this tag for questions where you are interested in the formal specification for a certain behavior in the given programming language, even though your question might otherwise have no practical use, or if the code posted would not make sense in a real-world application.

Always combine this tag with a programming language tag.

6253 questions
2023
votes
8 answers

C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?

C++11 introduced a standardized memory model, but what exactly does that mean? And how is it going to affect C++ programming? This article (by Gavin Clarke who quotes Herb Sutter) says that, The memory model means that C++ code now has a…
Nawaz
  • 327,095
  • 105
  • 629
  • 812
827
votes
6 answers

In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

Consider the main axis and cross axis of a flex container:                                                                                                                                                    Source: W3C To align flex items along the…
Michael Benjamin
  • 265,915
  • 79
  • 461
  • 583
459
votes
14 answers

How many levels of pointers can we have?

How many pointers (*) are allowed in a single variable? Let's consider the following example. int a = 10; int *p = &a; Similarly we can have int **q = &p; int ***r = &q; and so on. For example, int ****************zz;
Parag
  • 7,156
  • 9
  • 21
  • 28
348
votes
20 answers

int a[] = {1,2,}; Weird comma allowed. Any particular reason?

Maybe I am not from this planet, but it would seem to me that the following should be a syntax error: int a[] = {1,2,}; //extra comma in the end But it's not. I was surprised when this code compiled on Visual Studio, but I have learnt not to trust…
Armen Tsirunyan
  • 120,726
  • 52
  • 304
  • 418
301
votes
7 answers

Can C++ code be valid in both C++03 and C++11 but do different things?

Is it possible for C++ code to conform to both the C++03 standard and the C++11 standard, but do different things depending on under which standard it is being compiled?
Erik Sjölund
  • 9,109
  • 7
  • 34
  • 60
290
votes
1 answer

A positive lambda: '+[]{}' - What sorcery is this?

In Stack Overflow question Redefining lambdas not allowed in C++11, why?, a small program was given that does not compile: int main() { auto test = []{}; test = []{}; } The question was answered and all seemed fine. Then came Johannes…
Daniel Frey
  • 52,317
  • 11
  • 110
  • 168
289
votes
12 answers

Is main a valid Java identifier?

One of my kids is taking Java in high school and had this on one of his tests: Which of the following is a valid identifier in Java? a. 123java b. main c. java1234 d. {abce e. )whoot He answered b and got it wrong. I looked at the question…
Gary Bak
  • 4,564
  • 4
  • 19
  • 32
274
votes
11 answers

Why is f(i = -1, i = -1) undefined behavior?

I was reading about order of evaluation violations, and they give an example that puzzles me. 1) If a side effect on a scalar object is un-sequenced relative to another side effect on the same scalar object, the behavior is undefined. // snip f(i…
Nicu Stiurca
  • 8,024
  • 5
  • 37
  • 46
233
votes
3 answers

What are Rust's exact auto-dereferencing rules?

I'm learning/experimenting with Rust, and in all the elegance that I find in this language, there is one peculiarity that baffles me and seems totally out of place. Rust automatically dereferences pointers when making method calls. I made some tests…
kFYatek
  • 4,043
  • 4
  • 18
  • 14
208
votes
8 answers

Valid, but worthless syntax in switch-case?

Through a little typo, I accidentally found this construct: int main(void) { char foo = 'c'; switch(foo) { printf("Cant Touch This\n"); // This line is Unreachable case 'a': printf("A\n"); break; case 'b':…
abelenky
  • 58,532
  • 22
  • 99
  • 149
192
votes
4 answers

What made i = i++ + 1; legal in C++17?

Before you start yelling undefined behaviour, this is explicitly listed in N4659 (C++17) i = i++ + 1; // the value of i is incremented Yet in N3337 (C++11) i = i++ + 1; // the behavior is undefined What changed? From what I can…
Passer By
  • 16,942
  • 5
  • 38
  • 81
180
votes
6 answers

Does C++11, 14, 17 or 20 introduce a standard constant for pi?

There is a rather silly problem with the number pi in C and C++. As far as I know M_PI defined in math.h is not required by any standard. New C++ standards introduced a lot of complicated math in the standard library - hyperbolic functions,…
Amomum
  • 5,447
  • 5
  • 27
  • 54
155
votes
8 answers

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

Updated, see below! I have heard and read that C++0x allows an compiler to print "Hello" for the following snippet #include int main() { while(1) ; std::cout << "Hello" << std::endl; } It apparently has something to do with…
Johannes Schaub - litb
  • 466,055
  • 116
  • 851
  • 1,175
154
votes
4 answers

Is the operation "false < true" well defined?

Does the C++ specification define: the existence of the 'less than' operator for boolean parameters, and if so, the result of the 4 parameter permutations? In other words, are the results from the following operations defined by the…
duncan
  • 2,233
  • 2
  • 14
  • 21
139
votes
13 answers

How do I make an infinite empty loop that won't be optimized away?

The C11 standard appears to imply that iteration statements with constant controlling expressions should not be optimized out. I'm taking my advice from this answer, which specifically quotes section 6.8.5 from the draft standard: An iteration…
nneonneo
  • 154,210
  • 32
  • 267
  • 343
1
2 3
99 100