feat: test decltype
parent
f2bdaae4bf
commit
bcc4720b80
|
@ -1,7 +1,31 @@
|
|||
#include "type_inference.h"
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
#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() {
|
||||
|
||||
const std::vector<int> 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");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue