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
101
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,770
  • 3
  • 25
  • 49
98
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
  • 14,123
  • 11
  • 61
  • 126
97
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
  • 286,269
  • 29
  • 621
  • 977
96
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" ; …
96
votes
2 answers

When do extra parentheses have an effect, other than on operator precedence?

Parentheses in C++ are used in many places: e.g. in function calls and grouping expressions to override operator precedence. Apart from illegal extra parentheses (such as around function call argument lists), a general -but not absolute- rule of C++…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
94
votes
3 answers

Trap representation

What is a "trap representation" in C (some examples might help)? Does this apply to C++? Given this code... float f=3.5; int *pi = (int*)&f; ... and assuming that sizeof(int) == sizeof(float), do f and *pi have the same binary…
Burt
  • 959
  • 1
  • 7
  • 6
94
votes
4 answers

Why do (only) some compilers use the same address for identical string literals?

https://godbolt.org/z/cyBiWY I can see two 'some' literals in assembler code generated by MSVC, but only one with clang and gcc. This leads to totally different results of code execution. static const char *A = "some"; static const char *B =…
94
votes
3 answers

Printing null pointers with %p is undefined behavior?

Is it undefined behavior to print null pointers with the %p conversion specifier? #include int main(void) { void *p = NULL; printf("%p", p); return 0; } The question applies to the C standard, and not to C implementations.
Dror K.
  • 1,989
  • 17
  • 26
93
votes
4 answers

"if" statement syntax differences between C and C++

if (1) int a = 2; This line of code is valid C++ code (it compiles at the very least) yet invalid C code (doesn't compile). I know there are differences between the languages but this one was unexpected. I always thought the grammar was if (expr)…
Tom Hickson
  • 1,095
  • 7
  • 12
93
votes
3 answers

Is it guaranteed to be safe to perform memcpy(0,0,0)?

I am not so well-versed in the C standard, so please bear with me. I would like to know if it is guaranteed, by the standard, that memcpy(0,0,0) is safe. The only restriction I could find is that if the memory regions overlap, then the behavior is…
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
92
votes
3 answers

When does type information flow backwards in C++?

I just watched Stephan T. Lavavej talk at CppCon 2018 on "Class Template Argument Deduction", where at some point he incidentally says: In C++ type information almost never flows backwards ... I had to say "almost" because there's one or two cases,…
Massimiliano
  • 7,842
  • 2
  • 47
  • 62
92
votes
6 answers

Can argc be zero on a POSIX system?

Given the standard definition for the main program: int main(int argc, char *argv[]) { ... } Under which circumstances can argc be zero on a POSIX system?
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
92
votes
2 answers

Why are async state machines classes (and not structs) in Roslyn?

Let’s consider this very simple async method: static async Task myMethodAsync() { await Task.Delay(500); } When I compile this with VS2013 (pre Roslyn compiler) the generated state-machine is a struct. private struct d__0 :…
gregkalapos
  • 3,529
  • 2
  • 19
  • 35
92
votes
8 answers

Can branches with undefined behavior be assumed unreachable and optimized as dead code?

Consider the following statement: *((char*)NULL) = 0; //undefined behavior It clearly invokes undefined behavior. Does the existence of such a statement in a given program mean that the whole program is undefined or that behavior only becomes…
usr
  • 168,620
  • 35
  • 240
  • 369
92
votes
2 answers

Error when using in-class initialization of non-static data member and nested class constructor

The following code is quite trivial and I expected that it should compile fine. struct A { struct B { int i = 0; }; B b; A(const B& _b = B()) : b(_b) {} }; I've tested this code with g++ version 4.7.2,…
etam1024
  • 843
  • 1
  • 7
  • 17