-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathconstexpr_std_max.cpp
More file actions
60 lines (43 loc) · 1.66 KB
/
constexpr_std_max.cpp
File metadata and controls
60 lines (43 loc) · 1.66 KB
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
/*
This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
GS
~ Dec. 2021 or Jan. 2022
Q: See if I can have compile-time constexpr variables assigned from `std::max()` output.
A: Yep! I can! It works.
That actually makes perfect sense because the `std::max()` function declarations here
(https://en.cppreference.com/w/cpp/algorithm/max) all have a `constexpr` version, which means that
the function can act like a compile-time macro and be optionally statically evaluated at
compile-time if possible--ie: if all inputs are also literals or `constexpr` (I think).
To compile and run (assuming you've already `cd`ed into this dir):
1. In C++
```
mkdir -p bin && g++ -Wall -Wextra -Werror -O3 -std=c++17 -save-temps=obj constexpr_std_max.cpp -o bin/a && bin/a
```
References:
1. https://en.cppreference.com/w/cpp/algorithm/max
*/
// C++ includes
#include <algorithm> // `std::max()`
#include <iostream> // For `std::cin`, `std::cout`, `std::endl`, etc.
// C includes
#include <cstdint> // For `uint8_t`, `int8_t`, etc.
#include <cstdio> // For `printf()`
// int main(int argc, char *argv[]) // alternative prototype
int main()
{
constexpr uint32_t NUM1 = 100;
constexpr uint32_t NUM2 = 105;
constexpr uint32_t NUM3 = 95;
constexpr uint32_t MAX_NUM = std::max({NUM1, NUM2, NUM3});
static_assert(NUM1 <= MAX_NUM);
static_assert(NUM2 <= MAX_NUM);
static_assert(NUM3 <= MAX_NUM);
printf("All passed.\n");
return 0;
}
/*
SAMPLE OUTPUT:
In C++:
eRCaGuy_hello_world/cpp$ mkdir -p bin && g++ -Wall -Wextra -Werror -O3 -std=c++17 -save-temps=obj constexpr_std_max.cpp -o bin/a && bin/a
All passed.
*/