Creating a vector in C++ has never been easier
-
Creating a vector in C++ has never been easier
@vitaut Is there some magic here I'm too Rusty to understand? How is `v` in scope?
-
@vitaut Is there some magic here I'm too Rusty to understand? How is `v` in scope?
@jsbarretto it is captured by &
-
@jsbarretto it is captured by &
@vitaut ...before it has been defined? So it gets default-constructed, captured by the closure, then moved back into the original place? Gosh, that's... not nice
-
@funkylab captivating, innit?
@vitaut yes, it is.
I thought I was pretty on top of what happens here. But when I change the line to
`std::vector<int> v = [&]() -> std::vector<int> { v.push_back(1); return {}; }();`
then it stops working, making me think I have the value category of your original lambda's return value wrong. Hm. -
@vitaut ...before it has been defined? So it gets default-constructed, captured by the closure, then moved back into the original place? Gosh, that's... not nice
@jsbarretto The name becomes visible right after declaration. It's actually worse than you think because it is captured uninitialized.
-
Creating a vector in C++ has never been easier
@vitaut But...why
-
@vitaut But...why
@lisyarus ¯\_(ツ)_/¯
-
@vitaut yes, it is.
I thought I was pretty on top of what happens here. But when I change the line to
`std::vector<int> v = [&]() -> std::vector<int> { v.push_back(1); return {}; }();`
then it stops working, making me think I have the value category of your original lambda's return value wrong. Hm.@vitaut oh no. The program's output depends on whether I create `v1` after changing `v`, doesn't it… oh noooo
-
@vitaut oh no. The program's output depends on whether I create `v1` after changing `v`, doesn't it… oh noooo
-
@jsbarretto The name becomes visible right after declaration. It's actually worse than you think because it is captured uninitialized.
@vitaut Oh lawd, what a mess
-
Creating a vector in C++ has never been easier
The picture shows a C++ code snippet with the following content...
-
Creating a vector in C++ has never been easier
@vitaut "reference capture considered harmful"
-
@vitaut Oh lawd, what a mess
-
@jsbarretto The name becomes visible right after declaration. It's actually worse than you think because it is captured uninitialized.
@vitaut @jsbarretto The interaction with captures is cursed but you can do stuff like
Node n = {.value = 42, .next = &n}
in C to statically initialize self-referential pointer-based data structures. The more interesting case is something like
Node n[] = {{.value = 1, .next = &n[1]}, {.value = 2, .next = &n[0]}}
where you effectively get the linker/loader's relocation engine to deserialize cyclic pointer-based graphs. For statics you can also just extern declare the variables.
-
@vitaut @jsbarretto The interaction with captures is cursed but you can do stuff like
Node n = {.value = 42, .next = &n}
in C to statically initialize self-referential pointer-based data structures. The more interesting case is something like
Node n[] = {{.value = 1, .next = &n[1]}, {.value = 2, .next = &n[0]}}
where you effectively get the linker/loader's relocation engine to deserialize cyclic pointer-based graphs. For statics you can also just extern declare the variables.
@vitaut @jsbarretto Although this makes me realize I've never actually checked the spec wrt the semantics for stack/automatic storage duration variables for the self-referential initialization pattern (it's hard to see a use case) but I guess it's still alright?
-
undefined oblomov@sociale.network shared this topic on