From 90f78aa9ead2916099c9077bd827b17509df1267 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 19 Oct 2023 08:32:43 +0200 Subject: [PATCH] feat: add if/switch --- CMakeLists.txt | 3 ++- include/ifswitch.h | 7 +++++++ src/ifswitch.cpp | 25 +++++++++++++++++++++++++ src/main.cpp | 5 +++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 include/ifswitch.h create mode 100644 src/ifswitch.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d8f0d6..dec0741 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,8 @@ include(ExportCompileCommands) configure_file("${PROJECT_SOURCE_DIR}/include/version.h.in" "${PROJECT_BINARY_DIR}/include/version.h") -add_executable(hello src/main.cpp src/null.cpp src/constexp.cpp) +add_executable(hello src/main.cpp src/null.cpp src/constexp.cpp + src/ifswitch.cpp) target_compile_features(hello PUBLIC cxx_std_20) diff --git a/include/ifswitch.h b/include/ifswitch.h new file mode 100644 index 0000000..405756b --- /dev/null +++ b/include/ifswitch.h @@ -0,0 +1,7 @@ +#pragma once + +namespace ifswitch { + +void test(); + +}; diff --git a/src/ifswitch.cpp b/src/ifswitch.cpp new file mode 100644 index 0000000..995ff1a --- /dev/null +++ b/src/ifswitch.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +#include "ifswitch.h" + +void ifswitch::test() { + std::vector vec = {1, 2, 3, 4}; + + if (auto it = std::find(vec.begin(), vec.end(), 23); it != vec.end()) { + std::cout << "Found element 23 in vector" << std::endl; + } else { + std::cout << "No element 23 in vector" << std::endl; + } + + switch (auto it = vec.rbegin(); *it) { + case 4: + std::cout << "Found 4 at the back" << std::endl; + break; + default: + std::cout << "No 4 at the back" << std::endl; + break; + } +} diff --git a/src/main.cpp b/src/main.cpp index e7a4ebb..2830650 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include "version.h" #include "constexp.h" +#include "ifswitch.h" #include "null.h" int main(int argc, char **argv) { @@ -18,6 +19,10 @@ int main(int argc, char **argv) { << "Constexpr test:" << std::endl; constexp::test(); + std::cout << "\n" + << "Ifswitch test:" << std::endl; + ifswitch::test(); + std::cout << "\nExiting" << std::endl; return EXIT_SUCCESS;