1
0

feat: test decltype

This commit is contained in:
mandlm 2023-10-22 12:29:10 +02:00
parent f2bdaae4bf
commit bcc4720b80
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7

View File

@ -1,7 +1,31 @@
#include "type_inference.h" #include "type_inference.h"
#include <iostream> #include <iostream>
#include <type_traits>
#include <vector> #include <vector>
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<decltype(var), int>::value) {
std::cout << "int";
} else if (std::is_same<decltype(var), double>::value) {
std::cout << "double";
} else {
std::cout << "unknown";
}
std::cout << std::endl;
}
void TypeInferenceTest::run() { void TypeInferenceTest::run() {
const std::vector<int> int_vec = {1, 2, 3, 4, 5}; const std::vector<int> int_vec = {1, 2, 3, 4, 5};
@ -11,4 +35,12 @@ void TypeInferenceTest::run() {
std::cout << *it << " "; std::cout << *it << " ";
} }
std::cout << std::endl; 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");
} }