language-lawyer

Is it well-defined to use a pointer pointing to one-past-malloc?

≯℡__Kan透↙ 提交于 2021-02-05 20:13:19
问题 In C, it is perfectly well to make a pointer that points to one past the last element of an array and use it in pointer arithmetics, as long as you don't dereference it: int a[5], *p = a+5, diff = p-a; // Well-defined However, these are UBs: p = a+6; int b = *(a+5), diff = p-a; // Dereferencing and pointer arithmetic Now I have a question: Does this apply to dynamically allocated memory? Assume I'm only using a pointer pointing to one-past-the-last in pointer arithmetics, without

Is it well-defined to use a pointer pointing to one-past-malloc?

℡╲_俬逩灬. 提交于 2021-02-05 20:13:04
问题 In C, it is perfectly well to make a pointer that points to one past the last element of an array and use it in pointer arithmetics, as long as you don't dereference it: int a[5], *p = a+5, diff = p-a; // Well-defined However, these are UBs: p = a+6; int b = *(a+5), diff = p-a; // Dereferencing and pointer arithmetic Now I have a question: Does this apply to dynamically allocated memory? Assume I'm only using a pointer pointing to one-past-the-last in pointer arithmetics, without

Can the compiler optimize from heap to stack allocation?

断了今生、忘了曾经 提交于 2021-02-05 14:24:01
问题 As far as compiler optimizations go, is it legal and/or possible to change a heap allocation to a stack allocation? Or would that break the as-if rule? For example, say this is the original version of the code { Foo* f = new Foo(); f->do_something(); delete f; } Would a compiler be able to change this to the following { Foo f{}; f.do_something(); } I wouldn't think so, because that would have implications if the original version was relying on things like custom allocators. Does the standard

Can the compiler optimize from heap to stack allocation?

前提是你 提交于 2021-02-05 14:16:04
问题 As far as compiler optimizations go, is it legal and/or possible to change a heap allocation to a stack allocation? Or would that break the as-if rule? For example, say this is the original version of the code { Foo* f = new Foo(); f->do_something(); delete f; } Would a compiler be able to change this to the following { Foo f{}; f.do_something(); } I wouldn't think so, because that would have implications if the original version was relying on things like custom allocators. Does the standard

In CSS Flexbox, why are there no “justify-items” and “justify-self” properties?

懵懂的女人 提交于 2021-02-05 09:38:02
问题 Consider the main axis and cross axis of a flex container: Source: W3C To align flex items along the main axis there is one property: justify-content To align flex items along the cross axis there are three properties: align-content align-items align-self In the image above, the main axis is horizontal and the cross axis is vertical. These are the default directions of a flex container. However, these directions can be easily interchanged with the flex-direction property. /* main axis is

Does Lvalue-to-Rvalue conversion occur in function invocation?

混江龙づ霸主 提交于 2021-02-05 09:18:45
问题 Consider the below code: #include <iostream> int func(){ int a = 0; return a; } int main(){ int result = func(); } According to the cpp standard, some rules about the return statement are: A function returns to its caller by the return statement. [...] the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand So, the invocation for int result = func(); , as if it could be translate to: //a

User-Defined Deduction Guides in C++20

十年热恋 提交于 2021-02-05 05:57:18
问题 I'm working with std::variant and std::visit using the "overload" pattern that looks like this: #include <iostream> #include <variant> template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; int main(void) { std::variant<int, float> var; auto fs = overloaded { [](int var) {std::cout << "var is int" << std::endl;}, [](float var) {std::cout << "var is float" << std::endl;} }; var = 0; std::visit(fs, var); var =

“Nested” class-template argument deduction with parentheses: GCC vs. clang

佐手、 提交于 2021-02-05 04:59:20
问题 Related, but (IMHO) different: Nested template argument deduction for class templates not working The following C++17 code is rejected from GCC 8, but clang compiles it without any issues. The GCC's error message is included as a comment just before the problematic line. Which compiler is correct here? https://godbolt.org/z/WG6f7G template<class T> struct Foo { Foo(T) {} }; template<class T> struct Bar { Bar(T) {}; }; void works() { Bar bar{1};// {} Foo foo(bar);// () } void works_too() { Foo

Does std::thread::join guarantee writes visibility

一世执手 提交于 2021-02-05 04:43:48
问题 Std::thread::join is said to 'synchronize-with' the joined thread, however synchronization doesnt tell anything about visibility of side effects, it merely governs the order of the visiblity, ie. in following example: int g_i = 0; int main() { auto fn = [&] {g_i = 1;}; std::thread t1(fn); t1.join(); return g_i; } Do we have any guarantee in the c++ standard that this program will always return 1? 回答1: [thread.thread.member]: void join(); Effects : Blocks until the thread represented by *this

Why a template alias specialization depends on the context in which it is referred?

北慕城南 提交于 2021-02-04 19:00:09
问题 Consider this example code: template <class T> using pt_type = typename T::type; template <class T> class V { using type = int; public: using pt = pt_type<V>; }; void g() { V<int>::pt a; // Does compile pt_type<V<int>> b; // Does not compile } V<int>::pt is an alias for pt_type<V<int>> . Nevertheless the fact it is defined depends on the context where it is referred. Where is it explained in the C++ standard that the substitution of the template parameter by the template argument is performed