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
113
votes
2 answers

Is passing a C++ object into its own constructor legal?

I am surprised to accidentally discover that the following works: #include int main(int argc, char** argv) { struct Foo { Foo(Foo& bar) { std::cout << &bar << std::endl; } }; Foo foo(foo); // I can't…
Andrew Wagner
  • 17,943
  • 18
  • 70
  • 92
107
votes
2 answers

Does C++20 mandate source code being stored in files?

A slightly strange question, however, if I remember correctly, C++ source code doesn't require a file system to store its files. Having a compiler that scans handwritten papers via a camera would be a conforming implementation. Although practically…
JVApen
  • 10,085
  • 3
  • 26
  • 56
104
votes
9 answers

Have there ever been silent behavior changes in C++ with new standard versions?

(I'm looking for an example or two to prove the point, not a list.) Has it ever been the case that a change in the C++ standard (e.g. from 98 to 11, 11 to 14 etc.) changed the behavior of existing, well-formed, defined-behavior user code - silently?…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
103
votes
2 answers

Effectively final vs final - Different behavior

So far I thought that effectively final and final are more or less equivalent and that the JLS would treat them similar if not identical in the actual behavior. Then I found this contrived scenario: final int a = 97; System.out.println(true ? a :…
Zabuzard
  • 20,717
  • 7
  • 45
  • 67
103
votes
7 answers

Confusion about array initialization in C

In C language, if initialize an array like this: int a[5] = {1,2}; then all the elements of the array that are not initialized explicitly will be initialized implicitly with zeros. But, if I initialize an array like this: int…
msc
  • 30,333
  • 19
  • 96
  • 184
101
votes
6 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,257
  • 3
  • 18
  • 31
100
votes
8 answers

Is "else if" a single keyword?

I am new to C++. I often see conditional statement like below: if statement_0; else if statement_1; Question: Syntactically, shall I treat else if as a single keyword? Or is it actually an nested if statement within the outer else like below?…
modeller
  • 3,550
  • 3
  • 20
  • 37
99
votes
9 answers

Does "Undefined Behavior" really permit *anything* to happen?

The classic apocryphal example of "undefined behavior" is, of course, "nasal demons" — a physical impossibility, regardless of what the C and C++ standards permit. Because the C and C++ communities tend to put such an emphasis on the…
Kyle Strand
  • 14,120
  • 3
  • 59
  • 143
97
votes
7 answers

How can a program with a global variable called main instead of a main function work?

Consider following program: #include int main = ( std::cout << "C++ is excellent!\n", 195 ); Using g++ 4.8.1 (mingw64) on Windows 7 OS, the program compiles and runs fine, printing: C++ is excellent! to the console. main appears to be…
Destructor
  • 13,235
  • 8
  • 48
  • 108
96
votes
3 answers

When should I use @classmethod and when def method(self)?

While integrating a Django app I have not used before, I found two different ways used to define functions in classes. The author seems to use them both very intentionally. The first one is one I myself use a lot: class Dummy(object): def…
95
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
  • 14,982
  • 9
  • 48
  • 106
95
votes
3 answers

When is a private constructor not a private constructor?

Let's say I have a type and I want to make its default constructor private. I write the following: class C { C() = default; }; int main() { C c; // error: C::C() is private within this context (g++) // error:…
Barry
  • 247,587
  • 26
  • 487
  • 819
95
votes
8 answers

Efficient unsigned-to-signed cast avoiding implementation-defined behavior

I want to define a function that takes an unsigned int as argument and returns an int congruent modulo UINT_MAX+1 to the argument. A first attempt might look like this: int unsigned_to_signed(unsigned n) { return static_cast(n); } But as…
Nemo
  • 65,634
  • 9
  • 110
  • 142
94
votes
5 answers

C++ - Why static member function can't be created with 'const' qualifier

Today I got a problem. I am in the need of a static member function, const is not a must but a better. But, I didn't succeed in my efforts. Can anybody say why or how?
prabhakaran
  • 4,706
  • 13
  • 63
  • 103
94
votes
2 answers

Does this code from "The C++ Programming Language" 4th edition section 36.3.6 have well-defined behavior?

In Bjarne Stroustrup's The C++ Programming Language 4th edition section 36.3.6 STL-like Operations the following code is used as an example of chaining: void f2() { std::string s = "but I have heard it works even if you don't believe in it" ; …