language-lawyer Questions

1

Would like to exploit the following behaviour in Ruby ary = Array.new(5) { |i| [i, j=2*i, k=j+1] } p ary #> [[0, 0, 1], [1, 2, 3], [2, 4, 5], [3, 6, 7], [4, 8, 9]] It works for my purposes, b...
Higginbotham asked 11/7, 2020 at 20:52

2

Solved

#include <cstdio> #include <string> class A { std::string data; public: A() = default; explicit A (const char* data) : data(data) {} operator const char* () const; explicit o...

1

Consider the following example that compiles with clang but is rejected by edg, gcc and msvc. Demo #include <initializer_list> struct C { C(){} C(std::initializer_list<int> i = {3})...

1

Solved

I learned that when a class has a user-provided constructor then the compiler will not implicitly generate a default constructor. I wrote the following code, C d{}; in particular, and I expect it t...
Eyeopener asked 5/8, 2024 at 20:0

1

Solved

In addition to an implicitly-defined copy assignment operator, if a class also defines an operator= without an lvalue-reference object parameter, which of the operators must be selected? Please con...

2

C standard say (6.3.1.1 Boolean, characters, and integers): 1 Every integer type has an integer conversion rank defined as follows: No two signed integer types shall have the same rank, even if t...
Hokkaido asked 1/8, 2024 at 19:50

2

Solved

I recently learnt that constructors do not have names. I am also aware that a function has a type called a function type. For example, void func(int) { } In the above snippet, func has the functio...
Rime asked 3/4, 2022 at 9:59

1

I wrote the following program that compiles with EDG but is rejected by gcc, clang and msvc. Demo struct C { C(const C&&) = default; // EDG: ok, gcc: No, Clang: No }; int main() { } Afa...
Belk asked 31/7, 2024 at 16:17

3

Solved

Is the following C program guaranteed to exit with 0 or is the compiler allowed to identify the objects s and t with one another as is permitted in C++ as the so-called named return value optimizat...
Highspirited asked 27/7, 2024 at 21:25

2

Using -std=c++23, gcc accepts the following code while clang rejects it: struct outer { struct { int x; inline int get_x() const; } inner; }; inline int decltype(outer::inner)::get_x() const {...
Jabot asked 27/7, 2024 at 9:26

1

Solved

I'm seeing some unexpected behavior when using the std::variant::operator<. In the situation where the type has an implicit bool conversion operator and its less operator is not a member functio...
Scourge asked 26/7, 2024 at 10:58

2

Solved

In C++26, reading uninitialized variables is no longer undefined, it's "erroneous" now (What is erroneous behavior? How is it different from undefined behavior?). However, the wording for...
Objection asked 25/7, 2024 at 9:39

1

Solved

Consider the following two structs whose sizes are 8 and 1 bytes respectively: class eight { int i; char c; eight(const blub&) {} }; class one { char s; one(const blob&) {} }; Whe...
Anthropography asked 22/7, 2024 at 15:8

0

Generally _Atomic does not imply semantics of volatile, i.e. operations on the atomic object are not observable side effects that the compiler needs to preserve. As a consequence the compiler can o...
Quiroz asked 22/7, 2024 at 5:33

0

Generally std::atomic<T> does not imply semantics of volatile, i.e. operations on the atomic object are not observable side effects that the compiler needs to preserve. As a consequence the c...
Stopple asked 22/7, 2024 at 2:16

5

Solved

I'm having a disagreement with some co-workers over the following code: int foo ( int a, int b ) { return b > 0 ? a / b : a; } Does this code exhibit undefined behavior? EDIT: The disagreem...
Niphablepsia asked 21/10, 2016 at 9:9

1

I wrote the following program in c++23. Here I've overloaded member functions. But for all cases different compilers give different result as shown below in comment. As you can see I have three cas...
Knackwurst asked 17/7, 2024 at 7:39

1

Solved

Having class X, the following object initialization: new (ptr) X(X()); requires an accessible destructor even since C++17. Why is that, when the object is initialized by the default constructor di...
Serenata asked 15/7, 2024 at 13:9

4

Solved

Does it works correctly (does nothing) when I use vector<T> v; v.erase(v.end()); I want to use something like v.erase(std::find(...)); Should I if is it v.end() or not? There is no info ...
Paley asked 6/3, 2012 at 19:1

2

Solved

In this recent question, some code was shown to have undefined behavior: a[++i] = foo(a[i-1], a[i]); because even though the actual call of foo() is a sequence point, the assignment is unsequenc...
Brainwashing asked 22/8, 2017 at 10:0

2

There was some code floating around on Reddit that defined a member-function with an explicit this object parameter defined as type int. This made me wonder how this member-function could possibly ...
Dodgem asked 10/7, 2024 at 23:17

3

Solved

I have this example: #include <iostream> #define print(X) std::cout << X << std::endl struct B1 { B1(int _i = 5): i(_i) { print("B1 constructor"); }; int i; }; struc...

1

I am learning C++ using the books listed here. I wrote the following example(for purely academic reasons) that compiles with GCC but not with Clang and MSVC. Demo. struct C { static bool f() noexc...
Footmark asked 10/10, 2022 at 12:47

3

Solved

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...
Alter asked 28/4, 2013 at 5:46

1

I wrote the following program in C++23 that compiles with gcc and clang but is rejected by msvc. I want to know is this well-formed or ill-formed etc as per the standard. Live demo struct C { void...

© 2022 - 2025 — McMap. All rights reserved.