language-lawyer

Does UINT_MAX have all bits set to 1?

放肆的年华 提交于 2019-12-21 06:46:52
问题 This question is asked before but I am still confused. I know that unsigned int a = -1; would be UINT_MAX . But it is not because all bits of -1 is set. C11 says if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type So lets say UINT_MAX is 100 (I know it should be greater then 2^16-1 but lets ignore this for now) unsigned int a = -1; // will

Do we still need to write the empty angle brackets when using transparent std function objects?

大憨熊 提交于 2019-12-21 06:46:20
问题 With class template argument deduction we can write: std::less Fn; However, G++ 8.2 rejects this code: #include <algorithm> #include <vector> #include <functional> int main() { std::vector v= { 1, 3, 2, 7, 5, 4 }; std::sort(v.begin(),v.end(),std::greater()); } emitting the following error: error: cannot deduce template arguments for 'greater' from () Clang++ 7.0 and MSVC 15.8.0 compile it without warnings. Which compiler is right? 回答1: GCC is wrong. There is already a bug report. [dcl.type

Can a terminate handler throw an exception?

对着背影说爱祢 提交于 2019-12-21 05:06:48
问题 What is the defined behavior of the following program, if any? #include <iostream> #include <exception> #include <cstdlib> void i_throw() { std::cout << "i_throw()" << std::endl; // std::terminate() is noexcept so if the terminate handler throws... // then the terminate handler is called... // std::terminate is [[noreturn]] so don't return try { throw 7; } catch(...) { std::cout << "caught exception, re-throw()-ing" << std::endl; throw; } std::cout << "got here!" << std::endl; std::abort(); }

Undead objects ([basic.life]/8): why is reference rebinding (and const modification) allowed?

。_饼干妹妹 提交于 2019-12-21 05:03:22
问题 The "undead" clause I call the undead clause the C++ rule that after the destruction of an object, if a new object is created at the same address, it can sometimes be considered the same object as the old one. That rule always existed in C++ but with some changes on the additional conditions. I was made to read the latest undead clause by this question. The revised conditions in Lifetime [basic.life]/8 are: (8.1) the storage for the new object exactly overlays the storage location which the

Bitwise operators and signed types

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 05:03:05
问题 I'm reading C++ Primer and I'm slightly confused by a few comments which talk about how Bitwise operators deal with signed types. I'll quote: Quote #1 (When talking about Bitwise operators) "If the operand is signed and its value is negative, then the way that the “sign bit” is handled in a number of the bitwise operations is machine dependent. Moreover, doing a left shift that changes the value of the sign bit is undefined" Quote #2 (When talking about the rightshift operator) "If that

Is compiler allowed to call an immediate (consteval) function during runtime?

末鹿安然 提交于 2019-12-21 04:43:30
问题 This might be a stupid question, but I am confused. I had a feeling that an immediate ( consteval ) function has to be executed during compile time and we simply cannot see its body in the binary. This article clearly supports my feeling: This has the implication that the [immediate] function is only seen at compile time. Symbols are not emitted for the function, you cannot take the address of such a function, and tools such as debuggers will not be able to show them. In this matter,

Is what constitutes a failed initialization of block-scope static or thread storage duration variables underspecified?

那年仲夏 提交于 2019-12-21 04:31:26
问题 After answering this question and not finding a satisfying answer in the standard paper, I started wondering. The standard states the following w.r.t. initialization of mentioned variables: §6.7 [stmt.dcl] p4 [...] Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization . If the initialization exits by throwing an exception, the initialization is not complete, so it will

About cast in integer constant expression (in standard C)

自闭症网瘾萝莉.ら 提交于 2019-12-21 04:11:07
问题 In standard C (I mean C99 or C11) we have the so-called integer constant expressions , which are constant expressions whose operands are all constant integers. There are other constraints, as to avoid comma operators in the expression. However, other non-integer objects (even non-constant) are allowed in some special cases. For example, if the sizeof operator is applied to an object whose size is known in translation time, this is allowed as part of an integer constant expression (note that

Why template with only valid empty variadic pack ill formed?

a 夏天 提交于 2019-12-21 04:06:47
问题 What is the rationale of temp.res#8.3 (8) The validity of a template may be checked prior to any instantiation. [ Note: Knowing which names are type names allows the syntax of every template to be checked in this way. — end note ] The program is ill-formed, no diagnostic required, if: [..] (8.3) every valid specialization of a variadic template requires an empty template parameter pack, or That rule disallows trick as following to force template deduction as: template <typename ...Ts,

Should allocator construct() default initialize instead of value initializing?

不问归期 提交于 2019-12-21 03:46:37
问题 As a followup to this question, the default allocator ( std::allocator<T> ) is required to implement construct as follows (according to [default.allocator]): template <class U, class... Args> void construct(U* p, Args&&... args); Effects : ::new((void *)p) U(std::forward<Args>(args)...) That is, always value-initialization. The result of this is that std::vector<POD> v(num) , for any pod type, will value-initialize num elements - which is more expensive than default-initializing num elements.