1
0

feat: range based for

This commit is contained in:
mandlm 2023-10-22 22:12:56 +02:00
parent 09a1efeedf
commit f3c8cad317
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7
4 changed files with 40 additions and 4 deletions

View File

@ -1,12 +1,13 @@
add_library( add_library(
chapter_02 chapter_02
src/constexp.cpp
src/if_constexpr.cpp
src/ifswitch.cpp
src/initlist.cpp src/initlist.cpp
src/null.cpp src/null.cpp
src/constexp.cpp src/range_based_for.cpp
src/ifswitch.cpp
src/structured_binding.cpp src/structured_binding.cpp
src/type_inference.cpp src/type_inference.cpp)
src/if_constexpr.cpp)
target_compile_features(chapter_02 PUBLIC cxx_std_20) target_compile_features(chapter_02 PUBLIC cxx_std_20)

View File

@ -0,0 +1,9 @@
#pragma once
#include "chapter_interface.h"
class RangeBasedForTest : public ChapterTest {
public:
virtual void run() override;
virtual const char *name() const override { return "Range based for test"; }
};

View File

@ -0,0 +1,24 @@
#include <iostream>
#include <vector>
#include "range_based_for.h"
void RangeBasedForTest::run() {
std::vector<int> vals = {1, 2, 3, 4, 5};
std::cout << "vals: ";
for (auto val : vals) {
std::cout << val << " ";
}
std::cout << std::endl;
for (auto &val : vals) {
val *= 2;
}
std::cout << "vals: ";
for (auto val : vals) {
std::cout << val << " ";
}
std::cout << std::endl;
}

View File

@ -8,6 +8,7 @@
#include "ifswitch.h" #include "ifswitch.h"
#include "initlist.h" #include "initlist.h"
#include "null.h" #include "null.h"
#include "range_based_for.h"
#include "structured_binding.h" #include "structured_binding.h"
#include "type_inference.h" #include "type_inference.h"
#include "version.h" #include "version.h"
@ -26,6 +27,7 @@ int main(int argc, char **argv) {
chapter_2_tests.emplace_back(std::make_unique<StructuredBindingTest>()); chapter_2_tests.emplace_back(std::make_unique<StructuredBindingTest>());
chapter_2_tests.emplace_back(std::make_unique<TypeInferenceTest>()); chapter_2_tests.emplace_back(std::make_unique<TypeInferenceTest>());
chapter_2_tests.emplace_back(std::make_unique<IfConstexprTest>()); chapter_2_tests.emplace_back(std::make_unique<IfConstexprTest>());
chapter_2_tests.emplace_back(std::make_unique<RangeBasedForTest>());
for (auto &test : chapter_2_tests) { for (auto &test : chapter_2_tests) {
std::cout << "\n" << test->name() << ":" << std::endl; std::cout << "\n" << test->name() << ":" << std::endl;