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
67
votes
4 answers

Could it be the case that sizeof(T*) != sizeof(const T*)?

I'm arguing with my boss about this. They say "Yes they can be different." Is it possible that sizeof(T*) != sizeof(const T*) for a type T?
P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
67
votes
8 answers

De Morgan's Law optimization with overloaded operators

Every programmer should know that: (De Morgan's Laws) Under some circumstances, in order to optimize the program, it may happen that compiler modifies (!p && !q) to (!(p || q)). The two expressions are equivalent, and it makes no difference…
66
votes
3 answers

In C++, does initializing a global variable with itself have undefined behaviour?

int i = i; int main() { int a = a; return 0; } int a = a surely has undefined behaviour (UB), and more details on it is in Is reading an uninitialized value always an undefined behaviour? Or are there exceptions to it?. But what about int i =…
Dan
  • 2,694
  • 1
  • 6
  • 19
66
votes
3 answers

Fortran: integer*4 vs integer(4) vs integer(kind=4)

I'm trying to learn Fortran and I'm seeing a lot of different definitions being passed around and I'm wondering if they're trying to accomplish the same thing. What is the difference between the following? integer*4 integer(4) integer(kind=4)
Sam
  • 1,107
  • 2
  • 11
  • 7
66
votes
2 answers

Why isn't `std::initializer_list` defined as a literal type?

This is a follow-up of this question: Is it legal to declare a constexpr initializer_list object?. Since C++14, the std::initializer_list class has all of its methods marked with constexpr. It seems natural to be able to initialize an instance by…
ChristopherC
  • 1,635
  • 16
  • 31
66
votes
1 answer

Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++14?

As covered in Does initialization entail lvalue-to-rvalue conversion? Is int x = x; UB? the C++ standard has a surprising example in section 3.3.2 Point of declaration in which an int is initialized with it's own indeterminate value: int x = 12; {…
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
66
votes
2 answers

reinterpret_cast between char* and std::uint8_t* - safe?

Now we all sometimes have to work with binary data. In C++ we work with sequences of bytes, and since the beginning char was the our building block. Defined to have sizeof of 1, it is the byte. And all library I/O functions use char by default. All…
user3624760
65
votes
3 answers

Can the compiler optimize from heap to stack allocation?

As far as compiler optimizations go, is it legal and/or possible to change a heap allocation to a stack allocation? Or would that break the as-if rule? For example, say this is the original version of the code { Foo* f = new Foo(); …
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
65
votes
5 answers

Pure virtual functions may not have an inline definition. Why?

Pure virtual functions are those member functions that are virtual and have the pure-specifier ( = 0; ) Clause 10.4 paragraph 2 of C++03 tells us what an abstract class is and, as a side note, the following: [Note: a function declaration cannot…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
64
votes
3 answers

Why are anonymous namespaces not a sufficient replacement for namespace-static, according to the standards committee?

According to this answer, namespace-scoped static variables were undeprecated in C++11. That is, they were deprecated in C++03, because anonymous namespaces were considered better. But C++11 undeprecated them. Why? N3296 lists the reasoning for this…
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
64
votes
5 answers

Are int8_t and uint8_t intended to be char types?

Given this C++11 program, should I expect to see a number or a letter? Or not make expectations? #include #include int main() { int8_t i = 65; std::cout << i; } Does the standard specify whether this type can or will…
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
64
votes
3 answers

Using std::bind with member function, use object pointer or not for this argument?

When using std::bind to bind a member function, the first argument is the objects this pointer. However it works passing the object both as a pointer and not. See for example the following program: #include #include struct…
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
63
votes
5 answers

Is a C compiler allowed to coalesce sequential assignments to volatile variables?

I'm having a theoretical (non-deterministic, hard to test, never happened in practice) hardware issue reported by hardware vendor where double-word write to certain memory ranges may corrupt any future bus transfers. While I don't have any…
Andreas
  • 5,086
  • 3
  • 16
  • 36
63
votes
2 answers

Why does clang's stdbool.h contain #define false false

After being pointed there by a compiler error, I noticed clang's stdbool.h file includes (among other things) the following lines: #define bool bool #define false false #define true true They're contained in an #ifdef block that enforces…
anthonyvd
  • 7,329
  • 4
  • 30
  • 51
63
votes
5 answers

Redefining lambdas not allowed in C++11, why?

Example: #include int main() { auto test = []{}; test = []{}; return 0; } This emits the following error message in gcc 4.7.2: test.cpp: In function ‘int main()’: test.cpp:5:13: error: no match for ‘operator=’ in ‘test =…
OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96