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

C++20 bit_cast vs reinterpret_cast

According to the last meeting of the ISO C++ Committee, bit-cast will be introduced in C++20 standard. I know that reinterpret_cast is not suitable for this job due to type aliasing rules but my question is why did they choose not to extend the…
bogdan tudose
  • 1,064
  • 1
  • 9
  • 21
59
votes
3 answers

Addition assignment += behavior in expression

Recently I came across this question: Assignment operator chain understanding. While answering this question I started doubting my own understanding of the behavior of the addition assignment operator += or any other operator= (&=, *=, /=, etc.). My…
11thdimension
  • 10,333
  • 4
  • 33
  • 71
59
votes
4 answers

Why are arbitrary target expressions allowed in for-loops?

I accidentally wrote some code like this: foo = [42] k = {'c': 'd'} for k['z'] in foo: # Huh?? print k But to my surprise, this was not a syntax error. Instead, it prints {'c': 'd', 'z': 42}. My guess is that the code is translated literally…
jtbandes
  • 115,675
  • 35
  • 233
  • 266
59
votes
1 answer

Is stateful metaprogramming ill-formed (yet)?

One of my most beloved/evil inventions I've had the fortune to come across is the constexpr counter, aka stateful metaprogramming. As mentioned in the post, it seems to be legal under C++14, and I'm wondering has anything changed with C++17? The…
Passer By
  • 19,325
  • 6
  • 49
  • 96
59
votes
7 answers

The behaviour of floating point division by zero

Consider #include int main() { double a = 1.0 / 0; double b = -1.0 / 0; double c = 0.0 / 0; std::cout << a << b << c; // to stop compilers from optimising out the code. } I have always thought that a will be +Inf, b…
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
59
votes
2 answers

Why are are std::allocator's construct and destroy functions deprecated in c++17?

The c++17 specification deprecates the construct and destroy members of the std::allocator object. The working group provided rationale for deprecating other member functions here, under the heading "Deprecate the redundant members of…
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
59
votes
7 answers

C++ switch statement expression evaluation guarantee

Regarding switch the standard states the following. "When the switch statement is executed, its condition is evaluated and compared with each case constant." Does it mean that the condition expression evaluated once and once only, and it is…
mikk
  • 735
  • 6
  • 10
59
votes
6 answers

Does &((struct name *)NULL -> b) cause undefined behaviour in C11?

Code sample: struct name { int a, b; }; int main() { &(((struct name *)NULL)->b); } Does this cause undefined behaviour? We could debate whether it "dereferences null", however C11 doesn't define the term "dereference". 6.5.3.2/4 clearly…
M.M
  • 138,810
  • 21
  • 208
  • 365
59
votes
4 answers

Do distinct functions have distinct addresses?

Consider these two functions: void foo() {} void bar() {} is it guaranteed that &foo != &bar? Similarly, template void foo() { } is it guaranteed that &foo != &foo? There are two linkers I know of that fold function…
59
votes
3 answers

Does a declaration using "auto" match an extern declaration that uses a concrete type specifier?

Consider the following program: extern int x; auto x = 42; int main() { } Clang 3.5 accepts it (live demo), GCC 4.9 and VS2013 do not (live demo for the former). Who is right, and where is the correct behavior specified in the C++ Standard?
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
59
votes
2 answers

Incomplete type in nested name specifier

I tried to use incomplete type in nested name specifier as the following: class A; int b= A::c; // error: incomplete type ‘A’ used in nested name specifier class A { static const int c=5; }; There is says nothing about it in the 3.4.3/1 of…
user2953119
58
votes
3 answers

reinterpret_cast creating a trivially default-constructible object

cppreference† states that: Objects with trivial default constructors can be created by using reinterpret_cast on any suitably aligned storage, e.g. on memory allocated with std::malloc. This implies that the following is well-defined code: struct…
Barry
  • 286,269
  • 29
  • 621
  • 977
58
votes
4 answers

Is std::is_unsigned::value well defined?

I am wondering whether std::is_unsigned::value is well defined according to the standard or not? I ask the question because typename std::make_unsigned::type is not well defined.
Vincent
  • 57,703
  • 61
  • 205
  • 388
58
votes
7 answers

Is ++x %= 10 well-defined in C++?

While browsing the code of some project I came across the following statement: ++x %= 10; Is this statement well defined in C++ or does it fall into the same category as a[i] = i++ ?
kyku
  • 5,892
  • 4
  • 43
  • 51
58
votes
1 answer

What changes introduced in C++14 can potentially break a program written in C++11?

Introduction With the C++14 (aka. C++1y) Standard in a state close to being final, programmers must ask themselves about backwards compatibility, and issues related to such. The question In the answers of this question it is stated that the…
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196