UgMisc 0.3
Miscellaneous C++ header library
Loading...
Searching...
No Matches
int_finder.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: © 2026 Larry Chips <larry@larrychips.net>
3 * SPDX-Licence-Identifier: MIT
4 */
12#include "samples_output.hpp"
14
15#include <ugmisc/int_finder.hpp>
16
17#include <cstdint>
18
19
20
21
22template<typename PosType, auto MinHealth, auto MaxHealth, auto StartHealth=MaxHealth>
23struct Mob {
24 static_assert(MinHealth <= StartHealth);
25 static_assert(MaxHealth >= StartHealth);
26
29 MinHealth, MaxHealth, StartHealth
30 >;
31
32 using Position = PosType;
33
34 static constexpr Health maxHitPoints = (Health)MaxHealth;
35 static constexpr Health minHitPoints = (Health)MinHealth;
36
37 /*
38 * Actual data starts here.
39 */
40 Health hitPoints = (Health)StartHealth;
41 Position position;
42};
43
44
45
46
47template<auto MinX, auto MinY, auto MaxX, auto MaxY>
48struct Position {
51 MinX, MinY, MaxX, MaxY
52 >;
53
54 static constexpr Position minLimit = { (Coord)MinX, (Coord)MinY };
55 static constexpr Position maxLimit = { (Coord)MaxX, (Coord)MaxY };
56
57 Coord x, y;
58};
59
60
61
62int main(int, char**) {
63 SampleOutput out;
64 auto always = out.always();
65 auto expect = out.expect();
66
67 /*
68 * This should cause Enemy::Position::Coord to be int16_t.
69 * Health will be uint16_t.
70 */
71 using Enemy = Mob<Position<-200, -200, 200, 200>, 0, 300>;
72
73 always << "Enemy = Mob<Position<-200, -200, 200, 200>, 0, 300>\n"
74 "Enemy::Health: ";
75 out << getTypeName<typename Enemy::Health>() << "\n";
76 expect << getTypeName<std::uint16_t>() << "\n";
77
78 always << "Enemy::Position::Coord: ";
79 out << getTypeName<typename Enemy::Position::Coord>() << "\n";
80 expect << getTypeName<std::int16_t>() << "\n";
81
82 return 0;
83}
Obtain an integer type from its width, or from some values that must be representable.
@ SELECT_VALUE_SIGN
typename least_required_int_type_finder<>::template type< V... > least_required_int_type