From 5a3798a6042d2dee5183fe3229a60db2061a1500 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Fri, 20 Oct 2023 14:27:28 +0200 Subject: [PATCH] feat: add initializer list --- CMakeLists.txt | 2 +- include/initlist.h | 5 +++++ src/initlist.cpp | 22 ++++++++++++++++++++++ src/main.cpp | 8 ++++++-- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 include/initlist.h create mode 100644 src/initlist.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6508d20..e5f9770 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ 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 - src/ifswitch.cpp) + src/ifswitch.cpp src/initlist.cpp) target_compile_features(hello PUBLIC cxx_std_20) diff --git a/include/initlist.h b/include/initlist.h new file mode 100644 index 0000000..aa02a20 --- /dev/null +++ b/include/initlist.h @@ -0,0 +1,5 @@ +#pragma once + +namespace initlist { +void test(); +}; diff --git a/src/initlist.cpp b/src/initlist.cpp new file mode 100644 index 0000000..01c0c02 --- /dev/null +++ b/src/initlist.cpp @@ -0,0 +1,22 @@ +#include "initlist.h" +#include +#include +#include + +class InitList { +public: + std::vector _list; + + InitList(std::initializer_list list) : _list(list) {} +}; + +void initlist::test() { + + InitList initList{1, 2, 3, 4, 5}; + + std::cout << "InitList: "; + for (const auto &item : initList._list) { + std::cout << item << " "; + } + std::cout << std::endl; +} diff --git a/src/main.cpp b/src/main.cpp index 2830650..8dd124e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,11 @@ #include #include -#include "version.h" - #include "constexp.h" #include "ifswitch.h" +#include "initlist.h" #include "null.h" +#include "version.h" int main(int argc, char **argv) { @@ -23,6 +23,10 @@ int main(int argc, char **argv) { << "Ifswitch test:" << std::endl; ifswitch::test(); + std::cout << "\n" + << "Initializer list test:" << std::endl; + initlist::test(); + std::cout << "\nExiting" << std::endl; return EXIT_SUCCESS;