Questions tagged [language-lawyer]

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

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.

7856 questions
2188
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
  • 353,942
  • 115
  • 666
  • 851
1065
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 main axis there is one property: justify-content To align flex items along the cross axis there are three…
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
474
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,746
  • 9
  • 24
  • 29
361
votes
21 answers

int a[] = {1,2,}; Why is a trailing comma in an initializer-list allowed?

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
  • 130,161
  • 59
  • 324
  • 434
327
votes
4 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
  • 5,503
  • 4
  • 21
  • 14
324
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
  • 55,810
  • 13
  • 122
  • 180
302
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
  • 10,690
  • 7
  • 46
  • 74
290
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,746
  • 4
  • 22
  • 39
281
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,747
  • 8
  • 40
  • 48
210
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
  • 63,815
  • 23
  • 109
  • 159
196
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
  • 19,325
  • 6
  • 49
  • 96
194
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
  • 6,217
  • 8
  • 34
  • 62
178
votes
1 answer

What happens if you static_cast invalid value to enum class?

Consider this C++ code: enum class Color : char { red = 0x1, yellow = 0x2 } // ... char *data = ReadFile(); Color color = static_cast(data[0]); Suppose that data[0] is actually 100. What is color set to according to the standard? In…
darth happyface
  • 2,687
  • 2
  • 20
  • 15
165
votes
9 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
  • 496,577
  • 130
  • 894
  • 1,212
155
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,323
  • 3
  • 17
  • 21
1
2 3
99 100