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
150
votes
11 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
  • 171,345
  • 36
  • 312
  • 383
149
votes
4 answers

Is a null reference possible?

Is this piece of code valid (and defined behavior)? int &nullReference = *(int*)0; Both g++ and clang++ compile it without any warning, even when using -Wall, -Wextra, -std=c++98, -pedantic, -Weffc++... Of course the reference is not actually null,…
peoro
  • 25,562
  • 20
  • 98
  • 150
146
votes
5 answers

Accessing inactive union member and undefined behavior?

I was under the impression that accessing a union member other than the last one set is UB, but I can't seem to find a solid reference (other than answers claiming it's UB but without any support from the standard). So, is it undefined behavior?
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
141
votes
4 answers

C++ zero initialization - Why is `b` in this program uninitialized, but `a` is initialized?

According to the accepted (and only) answer for this Stack Overflow question, Defining the constructor with MyTest() = default; will instead zero-initialize the object. Then why does the following, #include struct foo { foo() =…
Duck Dodgers
  • 3,409
  • 8
  • 29
  • 43
139
votes
1 answer

Copy/move assignment in std::vector::erase() and std::deque::erase()

In the process of answering another question I stumbled upon slightly different wordings for std::vector::erase() and std::deque::erase(). This is what C++14 says about std::deque::erase ([deque.modifiers]/4-6, emphasis mine): Effects:…
Anton Savin
  • 40,838
  • 8
  • 54
  • 90
136
votes
3 answers

How does this piece of code determine array size without using sizeof( )?

Going through some C interview questions, I've found a question stating "How to find the size of an array in C without using the sizeof operator?", with the following solution. It works, but I cannot understand why. #include int main() { …
janojlic
  • 1,299
  • 1
  • 11
  • 10
134
votes
8 answers

What is "callback hell" and how and why does RX solve it?

Can someone give a clear definition together with a simple example that explains what is a "callback hell" for someone who does not know JavaScript and node.js ? When (in what kind of settings) does the "callback hell problem" occur? Why does it…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
134
votes
9 answers

Is it safe to push_back an element from the same vector?

vector v; v.push_back(1); v.push_back(v[0]); If the second push_back causes a reallocation, the reference to the first integer in the vector will no longer be valid. So this isn't safe? vector v; v.push_back(1); v.reserve(v.size() +…
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
131
votes
3 answers

Is using if (0) to skip a case in a switch supposed to work?

I have a situation where I would like for two cases in a C++ switch statement to both fall through to a third case. Specifically, the second case would fall through to the third case, and the first case would also fall through to the third case…
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
129
votes
6 answers

Lambda returning itself: is this legal?

Consider this fairly useless program: #include int main(int argc, char* argv[]) { int a = 5; auto it = [&](auto self) { return [&](auto b) { std::cout << (a + b) << std::endl; return self(self); }; }; …
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
129
votes
2 answers

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

Consider the following code: #include struct foo { // (a): void bar() { std::cout << "gman was here" << std::endl; } // (b): void baz() { x = 5; } int x; }; int main() { foo* f = 0; f->bar(); // (a) …
127
votes
7 answers

Why does the delete[] syntax exist in C++?

Every time somebody asks a question about delete[] on here, there is always a pretty general "that's how C++ does it, use delete[]" kind of response. Coming from a vanilla C background what I don't understand is why there needs to be a different…
awiebe
  • 3,758
  • 4
  • 22
  • 33
127
votes
5 answers

Definition of "==" operator for Double

For some reason I was sneaking into the .NET Framework source for the class Double and found out that the declaration of == is: public static bool operator ==(Double left, Double right) { return left == right; } The same logic applies for every…
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
126
votes
2 answers

Lambda capture and parameter with same name - who shadows the other? (clang vs gcc)

auto foo = "You're using g++!"; auto compiler_detector = [foo](auto foo) { std::puts(foo); }; compiler_detector("You're using clang++!"); clang++ 3.6.0 and newer print out "You're using clang++!" and warn about the capture foo being unused. g++…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
123
votes
3 answers

std::ignore with structured bindings?

Prelude: std::tuple f(); std::tuple g(); C++1z will introduce syntax for structured bindings which will make it possible to write instead of int a, b, c; std::tie(a, b, c) = f(); something like auto [a, b, c] =…
jotik
  • 17,044
  • 13
  • 58
  • 123