From bcc4720b80465a2a48ae54009fb733c9f1060dbb Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sun, 22 Oct 2023 12:29:10 +0200 Subject: [PATCH] feat: test decltype --- chapters/chapter_02/src/type_inference.cpp | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/chapters/chapter_02/src/type_inference.cpp b/chapters/chapter_02/src/type_inference.cpp index 75b9db5..c98dcba 100644 --- a/chapters/chapter_02/src/type_inference.cpp +++ b/chapters/chapter_02/src/type_inference.cpp @@ -1,7 +1,31 @@ #include "type_inference.h" #include +#include #include +auto print_sum(auto x, auto y) { + + decltype(x + y) sum = x + y; + + std::cout << "sum is " << sum << std::endl; + + return sum; +} + +void print_decltype(auto var) { + std::cout << "variable with value " << var << " is of type "; + + if (std::is_same::value) { + std::cout << "int"; + } else if (std::is_same::value) { + std::cout << "double"; + } else { + std::cout << "unknown"; + } + + std::cout << std::endl; +} + void TypeInferenceTest::run() { const std::vector int_vec = {1, 2, 3, 4, 5}; @@ -11,4 +35,12 @@ void TypeInferenceTest::run() { std::cout << *it << " "; } std::cout << std::endl; + + auto result_1 = print_sum(10.2, 13.3); + print_decltype(result_1); + + auto result_2 = print_sum(10, 13); + print_decltype(result_2); + + print_decltype("23"); }