From f10d428cfab58548264ea54e6d6f26c65af36ef4 Mon Sep 17 00:00:00 2001 From: sha512sum Date: Tue, 2 Jul 2024 16:50:12 +0000 Subject: [PATCH] Add constexpr check example --- CMakeLists.txt | 14 ++++++----- examples/src/constexpr_check.cpp | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 examples/src/constexpr_check.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 25eb3b6..e35c8c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,11 +68,13 @@ if(ENABLE_EXAMPLES) file(GLOB EXAMPLES_SRC "examples/src/*.cpp") foreach(EXAMPLE_SRC ${EXAMPLES_SRC}) get_filename_component(EXAMPLE_NAME ${EXAMPLE_SRC} NAME_WE) - add_executable(${EXAMPLE_NAME} ${EXAMPLE_SRC}) - target_link_libraries(${EXAMPLE_NAME} cserver) - set_property(TARGET ${EXAMPLE_NAME} PROPERTY CXX_STANDARD 23) - set_target_properties(${EXAMPLE_NAME} PROPERTIES - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/examples/output/") -endforeach() + if(NOT ${EXAMPLE_NAME} STREQUAL "constexpr_check") + add_executable(${EXAMPLE_NAME} ${EXAMPLE_SRC}) + target_link_libraries(${EXAMPLE_NAME} cserver) + set_property(TARGET ${EXAMPLE_NAME} PROPERTY CXX_STANDARD 23) + set_target_properties(${EXAMPLE_NAME} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/examples/output/") + endif() + endforeach() endif() diff --git a/examples/src/constexpr_check.cpp b/examples/src/constexpr_check.cpp new file mode 100644 index 0000000..dec73c2 --- /dev/null +++ b/examples/src/constexpr_check.cpp @@ -0,0 +1,40 @@ +#include + + +struct SomeComponent { + static constexpr utempl::ConstexprString kName = "component"; + + constexpr SomeComponent(auto&) {}; +}; + + +struct SomeComponent2 { + static constexpr utempl::ConstexprString kName = "component2"; + + constexpr SomeComponent2(auto&) {}; +}; + +struct OtherComponent { + static constexpr utempl::ConstexprString kName = "other"; + + auto Foo(auto& context) -> void { + context.template FindComponent<"component2">(); // UB on runtime with old check + // Compile time error with new check(check via loopholes) + }; + + constexpr OtherComponent(auto& context) { + context.template FindComponent<"component">(); + Foo(context); + }; +}; + + +auto main() -> int { + cserver::ServiceContextBuilder{} + .AppendConfigParam<"threads", 8>() + .Append() + .Append() + .Append() + .Sort() + .Run(); +};