language-lawyer

Was `long` guaranteed to be as wide as `size_t`

青春壹個敷衍的年華 提交于 2020-03-01 05:15:08
问题 When looking for evidence of unsigned long being enough to hold size_t for the purpose of being argument to printf I ran into two fact(oid)s. First there's this answer stating that long is indeed not guaranteed to be large enough for size_t . On the other hand I saw this answer suggesting to use printf("%lu", (unsigned long)x) in pre C99, x being of size_t . So the question is could you assume or were you guaranteed that long were enough to hold size_t in pre C99 . The other question is

Properties of a pointer to a zero length array

强颜欢笑 提交于 2020-02-29 12:50:24
问题 Consider int main() { auto a = new int[0]; delete[] a; // So there's no memory leak } Between the copy initialisation and deletion, are you allowed to read the pointer at a + 1 ? Furthermore, does the language permit the compiler to set a to nullptr ? 回答1: Per recent CWG reflector discussion as a result of editorial issue 3178, new int[0] produces what is currently called a "past-the-end" pointer value. It follows that a cannot be null, and a + 1 is undefined by [expr.add]/4. 来源: https:/

what are the overload resolution rules of list-initialization

馋奶兔 提交于 2020-02-27 23:32:38
问题 there are some codes here #include <iostream> struct A { A(int) {} }; struct B { B(A) { std::cout<<"0"<<std::endl; } B(B const&) { std::cout << "1" << std::endl; } B(B&&) { std::cout << "2" << std::endl; } }; int main() { B b0{{0}}; // this is ok #1 B b( {0} ); //this is error #2 } g++ report: main.cpp: In function ‘int main()’: main.cpp:17:11: error: call of overloaded ‘B(<brace-enclosed initializer list>)’ is ambiguous B b({ 0 }); ^ main.cpp:12:2: note: candidate: B::B(B&&) B(B&&) { ^ main

what are the overload resolution rules of list-initialization

回眸只為那壹抹淺笑 提交于 2020-02-27 23:32:09
问题 there are some codes here #include <iostream> struct A { A(int) {} }; struct B { B(A) { std::cout<<"0"<<std::endl; } B(B const&) { std::cout << "1" << std::endl; } B(B&&) { std::cout << "2" << std::endl; } }; int main() { B b0{{0}}; // this is ok #1 B b( {0} ); //this is error #2 } g++ report: main.cpp: In function ‘int main()’: main.cpp:17:11: error: call of overloaded ‘B(<brace-enclosed initializer list>)’ is ambiguous B b({ 0 }); ^ main.cpp:12:2: note: candidate: B::B(B&&) B(B&&) { ^ main

The void(), the comma operator (operator,) and the impossible (?) overloading

感情迁移 提交于 2020-02-27 04:22:50
问题 Consider the following struct: struct S {}; In C++14, the definition below is valid: constexpr auto f() { return S{}, 'c'; } As well as the following one: constexpr auto f() { return S{}, void(); } Now, consider the following, working snippet that involves the first of the two definitions: #include<type_traits> struct S {}; constexpr int operator,(S, char) { return 42; } constexpr auto f() { return S{}, 'c'; } int main() { constexpr int i{f()}; static_assert(i == 42, "!"); static_assert(std:

The void(), the comma operator (operator,) and the impossible (?) overloading

落花浮王杯 提交于 2020-02-27 04:21:32
问题 Consider the following struct: struct S {}; In C++14, the definition below is valid: constexpr auto f() { return S{}, 'c'; } As well as the following one: constexpr auto f() { return S{}, void(); } Now, consider the following, working snippet that involves the first of the two definitions: #include<type_traits> struct S {}; constexpr int operator,(S, char) { return 42; } constexpr auto f() { return S{}, 'c'; } int main() { constexpr int i{f()}; static_assert(i == 42, "!"); static_assert(std:

Is converting an integer to a pointer always well defined?

偶尔善良 提交于 2020-02-21 13:56:33
问题 Is this valid C++? int main() { int *p; p = reinterpret_cast<int*>(42); } Assuming I never dereference p . Looking up the C++ standard, we have C++17 §6.9.2/3 [basic.compound] 3 Every value of pointer type is one of the following: a pointer to an object or function (the pointer is said to point to the object or function), or a pointer past the end of an object ([expr.add]), or the null pointer value ([conv.ptr]) for that type, or an invalid pointer value. A value of a pointer type that is a

Is converting an integer to a pointer always well defined?

孤者浪人 提交于 2020-02-21 13:45:36
问题 Is this valid C++? int main() { int *p; p = reinterpret_cast<int*>(42); } Assuming I never dereference p . Looking up the C++ standard, we have C++17 §6.9.2/3 [basic.compound] 3 Every value of pointer type is one of the following: a pointer to an object or function (the pointer is said to point to the object or function), or a pointer past the end of an object ([expr.add]), or the null pointer value ([conv.ptr]) for that type, or an invalid pointer value. A value of a pointer type that is a

Is converting an integer to a pointer always well defined?

隐身守侯 提交于 2020-02-21 13:43:05
问题 Is this valid C++? int main() { int *p; p = reinterpret_cast<int*>(42); } Assuming I never dereference p . Looking up the C++ standard, we have C++17 §6.9.2/3 [basic.compound] 3 Every value of pointer type is one of the following: a pointer to an object or function (the pointer is said to point to the object or function), or a pointer past the end of an object ([expr.add]), or the null pointer value ([conv.ptr]) for that type, or an invalid pointer value. A value of a pointer type that is a

Default behavior of copy module on user-defined classes

青春壹個敷衍的年華 提交于 2020-02-21 13:21:33
问题 When copy.copy or copy.deepcopy is called on an instance of a user-defined class that does not have a __copy__ or __deepcopy__ method, what does Python guarantee will happen? The official docs are disturbingly non-explicit on this matter. Will the function always just return a new instance of the same class with a shallow/deep copy of the original object's __dict__ (or whatever the equivalent is when __slots__ are involved)? Can the behavior differ between CPython, PyPy, etc.? Does the