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
93
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,970
  • 14
  • 26
92
votes
5 answers

Why "using namespace X;" is not allowed inside class/struct level?

class C { using namespace std; // error }; namespace N { using namespace std; // ok } int main () { using namespace std; // ok } Edit: Want to know motivation behind it.
iammilind
  • 62,239
  • 27
  • 150
  • 297
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,082
  • 2
  • 40
  • 56
92
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 =…
92
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
  • 65,583
  • 16
  • 147
  • 283
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
  • 6
  • 17
89
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
  • 162,013
  • 33
  • 219
  • 345
89
votes
3 answers

Is this a known pitfall of C++11 for loops?

Let's imagine we have a struct for holding 3 doubles with some member functions: struct Vector { double x, y, z; // ... Vector &negate() { x = -x; y = -y; z = -z; return *this; } Vector &normalize() { double s =…
ndkrempel
  • 1,871
  • 14
  • 18
88
votes
2 answers

Is it still safe to delete nullptr in c++0x?

In c++03 it is pretty clear that deleting a null pointer has no effect. Indeed, it is explicitly stated in §5.3.5/2 that: In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. However, in the…
Mankarse
  • 37,343
  • 9
  • 88
  • 138
88
votes
2 answers

Why do C and C++ allow the expression (int) + 4*5?

(int) + 4*5; Why is this (adding a type with a value) possible? (tried with g++ and gcc.) I know that it doesn't make sense (and has no effect), but I want to know why this is possible.
Ernest Bredar
  • 998
  • 4
  • 15
87
votes
6 answers

Why are references not "const" in C++?

We know that a "const variable" indicates that once assigned, you cannot change the variable, like this: int const i = 1; i = 2; The program above will fail to compile; gcc prompts with an error: assignment of read-only variable 'i' No problem, I…
Troskyvs
  • 5,653
  • 3
  • 23
  • 71
87
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,274
  • 1
  • 17
  • 32
85
votes
4 answers

Is it legal for source code containing undefined behavior to crash the compiler?

Let's say I go to compile some poorly-written C++ source code that invokes undefined behavior, and therefore (as they say) "anything can happen". From the perspective of what the C++ language specification deems acceptable in a "conformant"…
Jeremy Friesner
  • 57,675
  • 12
  • 103
  • 196
85
votes
3 answers

Is a pointer with the right address and type still always a valid pointer since C++17?

(In reference to this question and answer.) Before the C++17 standard, the following sentence was included in [basic.compound]/3: If an object of type T is located at an address A, a pointer of type cv T* whose value is the address A is said to…
Oliv
  • 16,492
  • 1
  • 24
  • 63
85
votes
4 answers

Different behaviour of comma operator in C++ with return?

This (note the comma operator): #include int main() { int x; x = 2, 3; std::cout << x << "\n"; return 0; } outputs 2. However, if you use return with the comma operator, this: #include int f() { return 2, 3;…
xyz
  • 3,119
  • 1
  • 18
  • 29