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

placement new on a class with reference field

This is a code example from the C++20 spec ([basic.life]/8): struct C { int i; void f(); const C& operator=( const C& ); }; const C& C::operator=( const C& other) { if ( this != &other ) { this->~C(); // lifetime of *this…
Amir Kirsh
  • 8,021
  • 22
  • 43
11
votes
5 answers

Is it legal to use \\ in // C++ comment? (LaTeX equation in C++ comment)

For documentation purpose I need to add some LaTeX equations in my C++ comments, by example: // // \begin{eqnarray*} // y_1 &=& x_1 \\ // y_2 &=& x_2 // \end{eqnarray*} // int main() {} With clang++ (version 9.0.1-10) I can compile my code…
Picaud Vincent
  • 8,544
  • 4
  • 19
  • 55
11
votes
1 answer

Compile-time elimination of if/else branch in C++

In the following code sample, the if statement depends on bool template parameter, which is a compile-time constant. Compilers handle this code differently: MSVC fails with link error (which is what I expected), because the template function in…
11
votes
1 answer

Can there be different implicit objects based on a later runtime decision in C++20?

This question refers to the addition of P0593 to the latest C++20 draft . Here is my example: #include #include void foo(void *p) { if ( std::getchar() == 'i' ) { *(int *)p = 2; std::printf("%d\n", *(int…
M.M
  • 130,300
  • 18
  • 171
  • 314
11
votes
1 answer

Unspecified Implicit Object Creation

Since P0593 Implicit creation of objects for low-level object manipulation has been accepted, objects may now be created implicitly in C++20. Specifically the wording introduced by the proposal allows certain operations (such as std::malloc) to…
walnut
  • 20,566
  • 4
  • 18
  • 54
11
votes
1 answer

g++ and clang++ different behaviour with template specialization for auto argument

Playing with C++17 auto template arguments I've encountered another g++/clang++ disagreement. Given the following simple code template struct foo; template struct foo { }; int main () { foo<42l> f42; // <--- long constant,…
max66
  • 60,491
  • 9
  • 65
  • 95
11
votes
1 answer

What's the meaning of the highlighted sentence below in [over.load]/1?

What is the meaning of the highlighted sentence below? Does it have anything to do with function templates? [over.load]/1: Not all function declarations can be overloaded. Those that cannot be overloaded are specified here. A program is…
11
votes
1 answer

C++20 contracts and unused variables

Apologies if this has been covered elsewhere. One of my frustrations has been that whenever I'm trying to check a post-condition of calling a function, I often must decorate the return variable as unused to avoid compiler warnings: auto const count…
KyleKnoepfel
  • 1,240
  • 6
  • 23
11
votes
3 answers

Does std::memcpy make its destination determinate?

Here is the code: unsigned int a; // a is indeterminate unsigned long long b = 1; // b is initialized to 1 std::memcpy(&a, &b, sizeof(unsigned int)); unsigned int c = a; // Is this not undefined behavior? (Implementation-defined…
Joel
  • 1,868
  • 1
  • 17
  • 29
11
votes
1 answer

"temporary of type 'A' has protected destructor", but its type is B

In the following code, compiled with Clang 8.0.0+ and -std=c++17, creating a derived class instance using B{} gives an error error: temporary of type 'A' has protected destructor. Why is A appearing in this message when the temporary has type B (and…
jtbandes
  • 104,858
  • 34
  • 217
  • 242
11
votes
1 answer

Multiple cv-decompositions of a type

The example in [conv.qual]/1 says that the type const int ** has two cv-decompositions. A cv-decomposition of a type T is a sequence of cv_i and P_i such that T is “cv_0 P_0 cv_1 P_1 ⋯ cv_{n−1} P_{n−1} cv_n U” for n≥0, where each cv_i is a set of…
Jan Tušil
  • 898
  • 5
  • 15
11
votes
1 answer

Is indexing a string literal an initializer constant expression?

The following code attempts to use array indexing on a string literal in two different constant contexts: static char x = "abcx"[3]; _Static_assert ("abcx"[3] == 'x', "..."); Judging by Compiler Explorer, there's clear consensus among tool vendors…
Leushenko
  • 11,633
  • 9
  • 44
  • 84
11
votes
1 answer

Are there any guarantees about consistency of __LINE__ directives?

GCC 9 has recently changed the behavior of the __LINE__ directive in some cases. The program below illustrates the change: #include #define expand() __LINE__ int main() { printf("%d\n",expand( )); return 0; } Because…
anol
  • 6,712
  • 1
  • 27
  • 68
11
votes
1 answer

Why does the C++ standard require the `Clock::now` function to be `static`?

With C++11, C++ has some timing facilities in the standard. One of these facilities is a standard interface for clocks, that basically allows getting the time at the call of the now function of the clock. All is well up until this point but I fail…
Fatih BAKIR
  • 2,499
  • 1
  • 13
  • 18
11
votes
1 answer

What's wrong with std::valarray's operator*?

Consider the following MCVE, where I have two value arrays where w is two times v (try it out here): #include using namespace std; int main() { valarray v { 1, 2, 3 }; for ([[maybe_unused]] auto x : v) {} // Ok auto w = v *…
andreee
  • 3,626
  • 16
  • 33
1 2 3
99
100