forked from furkankirac/cs409-2023-24-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
week12-app1.cpp
65 lines (45 loc) · 1.17 KB
/
week12-app1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// std::integral_constant, std::true_type, std::false_type, tag dispatching
// constexpr functions
// std::is_constant_evaluated()
// godbolt.org
// factorial function that runs at compile time
// - with integral_constant
// - with constexpr
// decltype(auto)
#include <iostream>
// #include <string>
using namespace std;
struct LoggingMode { };
struct ReleaseMode { };
// tag dispatching
void log(auto smt, LoggingMode) { cout << "Logging: " << smt << endl; }
void log(auto smt, ReleaseMode) { cout << "Release: " << smt << endl; }
struct int_10 {
using type = int;
static constexpr int value = 10;
};
struct float_3_14f {
using type = float;
static constexpr int value = 3.14f;
};
template<auto v>
struct Constant {
using type = decltype(v);
static constexpr decltype(v) value = v;
};
constexpr auto foo() -> int
{
return 10;
}
constexpr int i = 20;
int main() {
auto v = int_10::value;
auto v2 = Constant<10>::value;
auto v3 = std::integral_constant<int, 10>::value;
log("Whatever", LoggingMode{});
log("Hi there", ReleaseMode{});
int* p = (int*)&i;
cout << foo() << endl;
using T = decltype(foo());
return 0;
}