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
91
votes
6 answers

Pure Functions: Does "No Side Effects" Imply "Always Same Output, Given Same Input"?

The two conditions that define a function as pure are as follows: No side effects (i.e. only changes to local scope are allowed) Always return the same output, given the same input If the first condition is always true, are there any times the…
Magnus
  • 6,791
  • 8
  • 53
  • 84
90
votes
2 answers

Capturing a reference by reference in a C++11 lambda

Consider this: #include #include std::function make_function(int& x) { return [&]{ std::cout << x << std::endl; }; } int main() { int i = 3; auto f = make_function(i); i = 5; f(); } Is this…
Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
89
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,349
  • 1
  • 23
  • 29
89
votes
9 answers

Can code that will never be executed invoke undefined behavior?

The code that invokes undefined behavior (in this example, division by zero) will never get executed, is the program still undefined behavior? int main(void) { int i; if(0) { i = 1/0; } return 0; } I think it still is…
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
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,906
  • 14
  • 18
88
votes
14 answers

Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not?

I have seen it asserted several times now that the following code is not allowed by the C++ Standard: int array[5]; int *array_begin = &array[0]; int *array_end = &array[5]; Is &array[5] legal C++ code in this context? I would like an answer with a…
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
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.
zvavybir
  • 1,034
  • 6
  • 15
87
votes
3 answers

Will consteval functions allow template parameters dependent on function arguments?

In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at…
Annyo
  • 1,387
  • 9
  • 20
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
  • 7,537
  • 7
  • 47
  • 115
87
votes
10 answers

What is the size of void?

What would this statement yield? void *p = malloc(sizeof(void)); Edit: An extension to the question. If sizeof(void) yields 1 in GCC compiler, then 1 byte of memory is allocated and the pointer p points to that byte and would p++ be incremented…
Lakshmi
  • 1,759
  • 4
  • 18
  • 23
85
votes
6 answers

Is `new` in `new int;` considered an operator?

The expression new int; such as in int * x = new int; is a new expression. The term "new operator" seems to be used interchangeably with "new expression", for example in this question : Difference between 'new operator' and 'operator new'? Is it…
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
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
  • 70,199
  • 15
  • 131
  • 234
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
  • 17,610
  • 1
  • 29
  • 72
85
votes
7 answers

What is difference between const and non const key?

What is the difference between the following two lines? map map_data; map map_data;
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
84
votes
4 answers

Initializing an array of zeroes

It is well known that missing initializers for an array of scalars are defaulted to zero. int A[5]; // Entries remain uninitialized int B[5]= { 0 }; // All entries set to zero But is this (below) guaranteed ? int C[5]= { }; // All entries set to…
user1196549