Add constexpr check example

This commit is contained in:
sha512sum 2024-07-02 16:50:12 +00:00
parent f3f1ac56c0
commit f10d428cfa
2 changed files with 48 additions and 6 deletions

View file

@ -68,11 +68,13 @@ if(ENABLE_EXAMPLES)
file(GLOB EXAMPLES_SRC "examples/src/*.cpp") file(GLOB EXAMPLES_SRC "examples/src/*.cpp")
foreach(EXAMPLE_SRC ${EXAMPLES_SRC}) foreach(EXAMPLE_SRC ${EXAMPLES_SRC})
get_filename_component(EXAMPLE_NAME ${EXAMPLE_SRC} NAME_WE) get_filename_component(EXAMPLE_NAME ${EXAMPLE_SRC} NAME_WE)
if(NOT ${EXAMPLE_NAME} STREQUAL "constexpr_check")
add_executable(${EXAMPLE_NAME} ${EXAMPLE_SRC}) add_executable(${EXAMPLE_NAME} ${EXAMPLE_SRC})
target_link_libraries(${EXAMPLE_NAME} cserver) target_link_libraries(${EXAMPLE_NAME} cserver)
set_property(TARGET ${EXAMPLE_NAME} PROPERTY CXX_STANDARD 23) set_property(TARGET ${EXAMPLE_NAME} PROPERTY CXX_STANDARD 23)
set_target_properties(${EXAMPLE_NAME} PROPERTIES set_target_properties(${EXAMPLE_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/examples/output/") RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/examples/output/")
endforeach() endif()
endforeach()
endif() endif()

View file

@ -0,0 +1,40 @@
#include <cserver/components/loggable_component_base.hpp>
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<SomeComponent>()
.Append<SomeComponent2>()
.Append<OtherComponent>()
.Sort()
.Run();
};