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
1 answer

What is a "prospective destructor" in C++20?

It seems that in C++20 something called a "prospective destructor" was introduced. In C++17 [class.dtor]: In a declaration of a destructor, the declarator is a function declarator (11.3.5) of the form ptr-declarator ( parameter-declaration-clause…
janekb04
  • 1,682
  • 4
  • 22
11
votes
1 answer

static_assert with SFINAE

The standard says in [temp.res]/8: No diagnostic shall be issued for a template definition for which a valid specialization can be generated. If no valid specialization can be generated for a template definition, and that template is not…
user11923373
  • 471
  • 1
  • 11
11
votes
1 answer

Member definition of partially specialized classes

I'm trying to define member functions of partially specialized class templates, but different compilers have wildly different opinions of what I'm allowed to do and why. Let's take this slowly and start with something that works for all main…
11
votes
1 answer

Detect if inherited function is overridden

I came up with the following code detecting at compile time if inherited functions are overridden in the derived class. It works with all major compilers - gcc/clang/msvc. But is this approach actually backed by the standard? #include…
amosk
  • 375
  • 1
  • 9
11
votes
1 answer

The point of destroying a temporary object when it created in a member-initializer

#include struct A{ A(int){ } ~A(){ std::cout<<"A destroy\n"; } }; struct B{ B(int){ std::cout<<"B construct\n"; } ~B(){ std::cout<<"B destroy\n"; } }; struct Content{ A…
xmh0511
  • 4,505
  • 3
  • 28
11
votes
2 answers

Is the initialization order of the vector elements guaranteed by the standard?

To fill a std::vector from std::cin, I usually write as following code: struct point{ int x, y; }; int main() { std::size_t n; std::cin>>n; std::vector vec(n); for (auto& p:vec) std::cin>>p.x>>p.y; …
PavelDev
  • 481
  • 3
  • 11
11
votes
3 answers

Why does map not include out_of_range?

Consider the following code that doesn't compile: #include //#include // uncommenting this works int main() { std::map test; try { test.at(10); } catch(std::out_of_range& e) { } return…
Creepsy
  • 309
  • 2
  • 11
11
votes
2 answers

Is incrementing/decrementing or adding an integer value to a pointer that is not pointing to an element in a sequence Undefined Behavior?

I know that pointers (to array element) and iterators can be incremented/decremented to walk a sequence of elements and can jump back-and-for elements in the sequence. But what will happen if I increment a pointer to a single object or add to it an…
Itachi Uchiwa
  • 1,659
  • 6
  • 20
11
votes
2 answers

What is the difference between non-type template parameters in C++17 and C++11?

Consider this code: using func = int (*)(int, int); template void do_something(int first, int second) {} int something(int first, int second) { return 42; } void f() { constexpr auto function = something; do_something(10,…
Ghasem Ramezani
  • 1,490
  • 6
  • 24
11
votes
2 answers

Do reference invalidation guarantees automatically apply to pointers?

Consider the following code: std::map m; int &ref = m[0]; int *ptr = &m[0]; m.insert({1,2}); std::cout << ref; // #1 std::cout << *ptr; // #2 For an associative container like std::map, the standard says: The insert and emplace…
cigien
  • 50,328
  • 7
  • 37
  • 78
11
votes
1 answer

Chained compound assignments with C++17 sequencing are still undefined behaviour?

Originally, I presented a more complicated example, this one was proposed by @n. 'pronouns' m. in a now-deleted answer. But the question became too long, see edit history if you are interested. Has the following program well-defined behaviour in…
Quimby
  • 8,111
  • 3
  • 18
  • 35
11
votes
2 answers

Why static method overrides base class non-static method?

struct B { void foo () {} }; struct D : B { using B::foo; static void foo () {} }; int main () { D obj; obj.foo(); // calls D::foo() !? } Member method and static member method are entirely different for 2 reasons: static method…
iammilind
  • 62,239
  • 27
  • 150
  • 297
11
votes
2 answers

Why is deleting a function necessary when you're defining customization point object?

From libstdc++ header: namespace ranges { namespace __cust_swap { template void swap(_Tp&, _Tp&) = delete; From MS-STL header: namespace ranges { namespace _Swap { template
Lewis Liman
  • 429
  • 3
  • 9
11
votes
0 answers

template function matching with specialization

I tried the following code with GCC 10.0 and Clang 10.0: template