Why You Should Write Code That You Should Never Write - Daisy Hollman - CppCon 2021

CppCon · 15K views · 2022 · 1:05:15

Open on YouTube

C, C++, Rust, Go & systems languagesProgramming language designtalk

Why Stack Overflow recommends it

Stack Overflow users link it as the source of the C++20 lambda and parameter-pack tricks they ask about, such as enumerating a pack or getting the last argument.

Recommended by 1 people across 1 different months, 2022 to 2023.

What it is

Daisy Hollman's CppCon 2021 talk dissects her popular "Cute C++ trick of the day" snippets, explaining how and why they work and which dark corners of the language they touch. She also covers what to write in production code to get the same effect, with tricks like enumerating a pack and indexing parameter packs via C++20 lambdas.

What people said

Recommends Oliver Schönrock · 2022-03 · Stack Overflow · on “Enumerating a pack”

I don't quite understand the base trick from Daisy Hollman's talk: [this video] Enumerating a pack using c++20 lamdas with explicit template arguments. #include <iostream> #include <utility> template <typename Function, typename ...Ts> void enumerate_pack(Function f, Ts... args) { [&]<std::size_t... Idxs>(std::index_sequence<Idxs...>) { (f(Idxs, args), ...); } (std::make_index_sequence<sizeof...(args)>{}); } int main() { enumerate_pack([](std::size_t i, auto arg) { std::cout << i << ": " << arg << "\n"; }, "hello", 42, "world", 73.2); } My problem with it is this part: (f(Idxs, args), ...); This looks to me like we are "passing a type to a function". OK, we are expanding the pack so it's a single

Cites Const · 2022-04 · Stack Overflow · on “GCC and MSVC complaints about getting the last element from the parameter pack?”

I was basically looking into the talk: [this video] Despite the typo in her shown code, I tried myself the code for better understanding. However MSVC and GCC was not happy to accept the code and the Clang does: [LINK] Here is the code: #include <iostream> #include <version> #include <type_traits> #include <utility> #include <string> using namespace std::string_literals; template<typename, std::size_t> concept Any = true; template<typename Callable, typename... Args> auto invoke_f_with_last(Callable&& func, Args&&... args) noexcept { return[&]<std::size_t... Idxs>(std::index_sequence<Idxs...>) { return [func](Any<Idxs> auto..., auto&& last) noexcept { return func(std::forward<decltype(last)>(last

Cites alfC · 2022-10 · Stack Overflow · on “Leaky Meyers Singleton: is it threadsafe?”

@ListOfArrays, not only that, some even reported static to be faster than the equivalent code with call_once. [this video]

Cites Passer By · 2023-02 · Stack Overflow · on “Specifying parameter pack length with concepts”

This is a snippet based on Daisy Hollman's talk [this video] #include<utility> using std::size_t; template<typename, size_t> concept any = true; template<size_t N> auto invokeN(auto f, auto... args) { return [&]<size_t... Is>(std::index_sequence<Is...>) { return [&](any<Is> auto..., auto n, auto...) { return f(n); }(args...); }(std::make_index_sequence<N>{}); } auto x = invokeN<2>([](auto x){ return x; }, 42., "420", 4200); // 4200 Which supposedly uses Is to specify the length of the first parameter pack and makes the remaining parameters deducible. Is this legal? gcc [LINK] and clang trunk [LINK] seems to be choking on this left and right. clang 15 [LINK] and lower seems to compile fine.