From 2610e4969584fdf0ccd9221cb6a4d6214d0403ad Mon Sep 17 00:00:00 2001 From: Richard Powell Date: Mon, 25 Dec 2023 07:03:06 -0800 Subject: [PATCH] Copying the initializer list. --- cmake/compiler.cmake | 3 +++ include/wxUI/ForEach.h | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cmake/compiler.cmake b/cmake/compiler.cmake index b90ef2c..653a7f7 100644 --- a/cmake/compiler.cmake +++ b/cmake/compiler.cmake @@ -9,3 +9,6 @@ macro(wxUI_SetupCompilerForTarget arg) set_property(TARGET ${arg} PROPERTY COMPILE_WARNING_AS_ERROR ON) endmacro() +# Adding address sanitizer (see https://stackoverflow.com/questions/44320465/whats-the-proper-way-to-enable-addresssanitizer-in-cmake-that-works-in-xcode) +add_compile_options(-fsanitize=address) +add_link_options(-fsanitize=address) diff --git a/include/wxUI/ForEach.h b/include/wxUI/ForEach.h index 28f971c..02a1997 100644 --- a/include/wxUI/ForEach.h +++ b/include/wxUI/ForEach.h @@ -26,6 +26,7 @@ SOFTWARE. #include "Layout.h" namespace wxUI::details { +// clang-format off // Big help to Dennis Kormalev (https://www.linkedin.com/in/dkormalev/) for the example at: // https://godbolt.org/z/sv5seP79q template @@ -34,12 +35,28 @@ struct CanApply : std::false_type { }; template struct CanApply, std::enable_if_t, void>> : std::true_type { }; +template +struct invoke_apply_result : std::invoke_result { }; + +template +struct invoke_apply_result, std::enable_if_t, void>> : std::invoke_result { }; + +template +using invoke_apply_result_t = typename invoke_apply_result::type; + +template +concept ForEachFunction = CreateAndAddable::type>; +// clang-format on + } namespace wxUI { +// clang-format off template +requires(details::ForEachFunction>) struct ForEach { + // clang-format on ForEach(Range&& args, Function&& createFunction) : args(std::forward(args)) @@ -72,11 +89,12 @@ struct ForEach { Function createFunction; }; +// initializer_list is like a string_view, we want a copy. So deduce as vector so we have a copy template -ForEach(std::initializer_list, Function) -> ForEach, Function>; +ForEach(std::initializer_list, Function&&) -> ForEach, Function>; template -ForEach(wxSizerFlags const& flags, std::initializer_list, Function) -> ForEach, Function>; +ForEach(wxSizerFlags const& flags, std::initializer_list, Function&&) -> ForEach, Function>; template auto VForEach(std::initializer_list args, Function&& function)