From 0c5d612867d4abf9c77f9f95e9841e59996cd08b Mon Sep 17 00:00:00 2001 From: sha512sum Date: Wed, 28 Feb 2024 23:18:15 +0000 Subject: [PATCH] Add Tuple Example --- CMakeLists.txt | 11 +++++++++++ examples/src/tuple.cpp | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 examples/src/tuple.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6639423..a18913a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,4 +18,15 @@ if(ENABLE_TESTS) include(GoogleTest) gtest_discover_tests(utempl_tests) endif() +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} utempl) + set_property(TARGET ${EXAMPLE_NAME} PROPERTY CXX_STANDARD 20) + set_target_properties(${EXAMPLE_NAME} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/examples/") +endforeach() +endif() target_include_directories(utempl INTERFACE include) diff --git a/examples/src/tuple.cpp b/examples/src/tuple.cpp new file mode 100644 index 0000000..f2e9d49 --- /dev/null +++ b/examples/src/tuple.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +auto main() -> int { + using utempl::literals::operator""_c; + constexpr utempl::Tuple tuple = utempl::Tuple{42, 3.141500, "Hello World"}; + utempl::Tuple tuple2{{}}; + std::ignore = tuple2; + std::cout << utempl::kTupleSize << std::endl; // Get tuple size + std::cout << tuple[0_c] << std::endl; // Get element using [] with literal + auto newTuple = utempl::Transform(tuple, utempl::Overloaded( + [](auto arg){ + return std::to_string(arg); + }, + [](const char* arg) { + return std::string(arg); + } + )); + auto flag = newTuple == utempl::Tuple{"42", "3.141500", "Hello World"}; + assert(flag); +};