feat: if constexpr
This commit is contained in:
parent
161e768069
commit
09a1efeedf
@ -1,6 +1,12 @@
|
|||||||
add_library(
|
add_library(
|
||||||
chapter_02 src/initlist.cpp src/null.cpp src/constexp.cpp src/ifswitch.cpp
|
chapter_02
|
||||||
src/structured_binding.cpp src/type_inference.cpp)
|
src/initlist.cpp
|
||||||
|
src/null.cpp
|
||||||
|
src/constexp.cpp
|
||||||
|
src/ifswitch.cpp
|
||||||
|
src/structured_binding.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)
|
||||||
|
|
||||||
|
9
chapters/chapter_02/include/if_constexpr.h
Normal file
9
chapters/chapter_02/include/if_constexpr.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "chapter_interface.h"
|
||||||
|
|
||||||
|
class IfConstexprTest : public ChapterTest {
|
||||||
|
public:
|
||||||
|
virtual void run() override;
|
||||||
|
virtual const char *name() const override { return "If constexpr test"; }
|
||||||
|
};
|
24
chapters/chapter_02/src/if_constexpr.cpp
Normal file
24
chapters/chapter_02/src/if_constexpr.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
#include "if_constexpr.h"
|
||||||
|
|
||||||
|
void print_decltype(auto var) {
|
||||||
|
std::cout << "variable with value " << var << " is of type ";
|
||||||
|
|
||||||
|
if constexpr (std::is_same<decltype(var), int>::value) {
|
||||||
|
std::cout << "int";
|
||||||
|
} else if constexpr (std::is_same<decltype(var), double>::value) {
|
||||||
|
std::cout << "double";
|
||||||
|
} else {
|
||||||
|
std::cout << "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IfConstexprTest::run() {
|
||||||
|
print_decltype(23);
|
||||||
|
print_decltype(23.5);
|
||||||
|
print_decltype("42");
|
||||||
|
}
|
@ -4,6 +4,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "constexp.h"
|
#include "constexp.h"
|
||||||
|
#include "if_constexpr.h"
|
||||||
#include "ifswitch.h"
|
#include "ifswitch.h"
|
||||||
#include "initlist.h"
|
#include "initlist.h"
|
||||||
#include "null.h"
|
#include "null.h"
|
||||||
@ -24,6 +25,7 @@ int main(int argc, char **argv) {
|
|||||||
chapter_2_tests.emplace_back(std::make_unique<InitListTest>());
|
chapter_2_tests.emplace_back(std::make_unique<InitListTest>());
|
||||||
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>());
|
||||||
|
|
||||||
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;
|
||||||
|
Reference in New Issue
Block a user