language-lawyer

Why is Boolean.prototype a Boolean object again? (And same for String and Number, but not Date or RegExp?)

时光怂恿深爱的人放手 提交于 2021-02-08 05:31:10
问题 In ES5, Boolean.prototype is a Boolean object: The Boolean prototype object is itself a Boolean object (its [[Class]] is "Boolean") whose value is false. In ES6 / ES2015, it isn't: The Boolean prototype object is an ordinary object. It is not a Boolean instance and does not have a [[BooleanData]] internal slot. In ES2016, it is once again: The Boolean prototype is itself a Boolean object; it has a [[BooleanData]] internal slot with the value false. (and it remains so in ES2017 as well.) The

Does forming a reference to an object constitute access?

微笑、不失礼 提交于 2021-02-07 20:30:49
问题 Does forming a reference to an object constitute access? Here's what GCC and Clang currently do: void test(int const volatile* ptr) noexcept { *ptr; // movl (%rdi), eax // Reads *ptr [[maybe_unused]] int const volatile& ref = *ptr; // Does not read *ptr } My question is specifically about the statement [[maybe_unused]] int const volatile& ref = *ptr; According to the abstract machine, does this read the value of the object pointed to by ptr ? Would this statement, in isolation, be undefined

Multiple structures in a single malloc invoking undefined behaviour?

假装没事ソ 提交于 2021-02-07 20:29:56
问题 From Use the correct syntax when declaring a flexible array member it says that when malloc is used for a header and flexible data when data[1] is hacked into the struct , This example has undefined behavior when accessing any element other than the first element of the data array. (See the C Standard, 6.5.6.) Consequently, the compiler can generate code that does not return the expected value when accessing the second element of data. I looked up the C Standard 6.5.6, and could not see how

Do `const T` and `T` have no difference when taking its nested type?

。_饼干妹妹 提交于 2021-02-07 18:44:00
问题 #include <string> template<typename T, typename C, typename CR> void f() { typename T::size_type* p1{}; // ok typename CR::size_type* p2{}; // error typename C::size_type* p3{}; // Does the C++ standard allow this? } int main() { f<std::string, const std::string, const std::string&>(); } Do const T and T have no difference when taking its nested type? 回答1: Indeed, the "nested types" are the same. A type qualified with const and/or volatile is a "version" of the unqualified type ([basic.type

Which way to test for signed integer overflow on multiply?

有些话、适合烂在心里 提交于 2021-02-07 14:53:52
问题 Which if any of the following does "the right thing" in a standards-compliant manner? You can assume that m and n are of type int (signed integer). The main issue is signed integer overflow. Sample 1. size_t bytes = n * m; if (n > 0 && m > 0 && SIZE_MAX/n >= m) { /* allocate “bytes” space */ } Sample 2. if (n > 0 && m > 0 && SIZE_MAX/n >= m) { size_t bytes = n * m; /* allocate “bytes” space */ } Sample 3. if (n > 0 && m > 0 && SIZE_MAX/n >= m) { size_t bytes = (size_t)n * (size_t)m; /*

Which way to test for signed integer overflow on multiply?

帅比萌擦擦* 提交于 2021-02-07 14:53:28
问题 Which if any of the following does "the right thing" in a standards-compliant manner? You can assume that m and n are of type int (signed integer). The main issue is signed integer overflow. Sample 1. size_t bytes = n * m; if (n > 0 && m > 0 && SIZE_MAX/n >= m) { /* allocate “bytes” space */ } Sample 2. if (n > 0 && m > 0 && SIZE_MAX/n >= m) { size_t bytes = n * m; /* allocate “bytes” space */ } Sample 3. if (n > 0 && m > 0 && SIZE_MAX/n >= m) { size_t bytes = (size_t)n * (size_t)m; /*

What is the difference between “const int& jj” and “int& const jj”?

↘锁芯ラ 提交于 2021-02-07 13:32:45
问题 I am confused with the two. I am aware of the C++ references which are inherently constant and once set they cannot be changed to refer to something else. 回答1: const int& means reference to const int . (Similarly, int& means reference to non-const int .) int& const literally means const reference (to non-const int ), which is invalid in C++, because reference itself can't be const-qualified. $8.3.2/1 References [dcl.ref] Cv-qualified references are ill-formed except when the cv-qualifiers are

What is the difference between “const int& jj” and “int& const jj”?

空扰寡人 提交于 2021-02-07 13:29:04
问题 I am confused with the two. I am aware of the C++ references which are inherently constant and once set they cannot be changed to refer to something else. 回答1: const int& means reference to const int . (Similarly, int& means reference to non-const int .) int& const literally means const reference (to non-const int ), which is invalid in C++, because reference itself can't be const-qualified. $8.3.2/1 References [dcl.ref] Cv-qualified references are ill-formed except when the cv-qualifiers are

Braced functional cast to reference type, a hole in the standard or compilers bug?

纵饮孤独 提交于 2021-02-07 12:52:15
问题 According to the standard, a braced functional cast always results in a prvalue, [expr.cast]/2 Otherwise, the expression is a prvalue of the specified type whose result object is direct-initialized with the initializer. Which is hard to interpret when the specified type is a reference type, as it may happen in generic programming. Compiler have adopted specific behavior in this case: #include <type_traits> struct A { A ()=default; A (const A&); }; template <class T, class U> decltype(auto) f

How to compare two standard conversion sequences use the rank of contained conversions

会有一股神秘感。 提交于 2021-02-07 12:45:15
问题 #include <iostream> void g(int*); //#1 void g(int (&arr)[2]); //#2 void f(int*); //#3 void f(int const*); //#4 int main(){ int arr[2] ={0}; f(arr); // choose #3 g(arr); //ambiguous } Consider the above code, #3 is seleteced for f(ptr) , however, g(arr) gives a ambiguous diagnostic. The rule for choosing the best function is defined as: Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if S1 is a proper subsequence of S2 (comparing the