language-lawyer

Construct an empty object without the default constructor

女生的网名这么多〃 提交于 2020-01-22 16:25:39
问题 Suppose I have a type F . I know that F is empty, but F has no default constructor, so I can't use F() to construct it. Is there a way to obtain a valid object of type F anyway? I seem to recall a mention that there was such a way with arcane usage of unions. Ideally, it would be constexpr friendly. This can be useful because captureless lambdas only gained a default constructor in C++20. In C++17, if I want to "pass a lambda to a template" and call that lambda without having an instance of

Construct an empty object without the default constructor

℡╲_俬逩灬. 提交于 2020-01-22 16:25:22
问题 Suppose I have a type F . I know that F is empty, but F has no default constructor, so I can't use F() to construct it. Is there a way to obtain a valid object of type F anyway? I seem to recall a mention that there was such a way with arcane usage of unions. Ideally, it would be constexpr friendly. This can be useful because captureless lambdas only gained a default constructor in C++20. In C++17, if I want to "pass a lambda to a template" and call that lambda without having an instance of

How do I convert an arbitrary double to an integer while avoiding undefined behavior?

有些话、适合烂在心里 提交于 2020-01-22 13:38:27
问题 Let's say I've got a function that accepts a 64-bit integer, and I want to call it with a double with arbitrary numeric value (i.e. it may be very large in magnitude, or even infinite): void DoSomething(int64_t x); double d = [...]; DoSomething(d); Paragraph 1 of [conv.fpint] in the C++11 standard says this: A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion trun- cates; that is, the fractional part is discarded. The behavior is undefined if

How do I convert an arbitrary double to an integer while avoiding undefined behavior?

偶尔善良 提交于 2020-01-22 13:38:05
问题 Let's say I've got a function that accepts a 64-bit integer, and I want to call it with a double with arbitrary numeric value (i.e. it may be very large in magnitude, or even infinite): void DoSomething(int64_t x); double d = [...]; DoSomething(d); Paragraph 1 of [conv.fpint] in the C++11 standard says this: A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion trun- cates; that is, the fractional part is discarded. The behavior is undefined if

Is the explanation of relaxed ordering erroneous in cppreference?

限于喜欢 提交于 2020-01-22 10:50:11
问题 In the documentation of std::memory_order on cppreference.com there is an example of relaxed ordering: Relaxed ordering Atomic operations tagged memory_order_relaxed are not synchronization operations; they do not impose an order among concurrent memory accesses. They only guarantee atomicity and modification order consistency. For example, with x and y initially zero, // Thread 1: r1 = y.load(std::memory_order_relaxed); // A x.store(r1, std::memory_order_relaxed); // B // Thread 2: r2 = x

If initialization or destruction is terminated by an exception which is not handled, are fully-constructed subobjects necessarily destroyed?

こ雲淡風輕ζ 提交于 2020-01-22 09:26:08
问题 The standard distinguishes between two forms of destruction that occur when an exception is thrown. Emphasis mine. §15.2/1 As control passes from a throw-expression to a handler, destructors are invoked for all automatic objects constructed since the try block was entered. The automatic objects are destroyed in the reverse order of the completion of their construction. §15.2/2 An object of any storage duration whose initialization or destruction is terminated by an exception will have

In overload resolution, does selection of a function that uses the ambiguous conversion sequence necessarily result in the call being ill-formed?

北战南征 提交于 2020-01-22 06:45:07
问题 The question arose while I was researching the answer to this SO question. Consider the following code: struct A{ operator char() const{ return 'a'; } operator int() const{ return 10; } }; struct B { void operator<< (int) { } }; int main() { A a; B b; b << a; } The conversion of a to int can be either via a.operator char() followed by an integral promotion, or a.operator int() followed by an identity conversion (i.e., no conversion at all). The standard says that (§13.3.3.1 [over.best.ics]

In overload resolution, does selection of a function that uses the ambiguous conversion sequence necessarily result in the call being ill-formed?

爱⌒轻易说出口 提交于 2020-01-22 06:44:45
问题 The question arose while I was researching the answer to this SO question. Consider the following code: struct A{ operator char() const{ return 'a'; } operator int() const{ return 10; } }; struct B { void operator<< (int) { } }; int main() { A a; B b; b << a; } The conversion of a to int can be either via a.operator char() followed by an integral promotion, or a.operator int() followed by an identity conversion (i.e., no conversion at all). The standard says that (§13.3.3.1 [over.best.ics]

Valid, but worthless syntax in switch-case?

微笑、不失礼 提交于 2020-01-22 04:17:25
问题 Through a little typo, I accidentally found this construct: int main(void) { char foo = 'c'; switch(foo) { printf("Cant Touch This\n"); // This line is Unreachable case 'a': printf("A\n"); break; case 'b': printf("B\n"); break; case 'c': printf("C\n"); break; case 'd': printf("D\n"); break; } return 0; } It seems that the printf at the top of the switch statement is valid, but also completely unreachable. I got a clean compile, without even a warning about unreachable code, but this seems

Valid, but worthless syntax in switch-case?

送分小仙女□ 提交于 2020-01-22 04:15:34
问题 Through a little typo, I accidentally found this construct: int main(void) { char foo = 'c'; switch(foo) { printf("Cant Touch This\n"); // This line is Unreachable case 'a': printf("A\n"); break; case 'b': printf("B\n"); break; case 'c': printf("C\n"); break; case 'd': printf("D\n"); break; } return 0; } It seems that the printf at the top of the switch statement is valid, but also completely unreachable. I got a clean compile, without even a warning about unreachable code, but this seems