-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
32 lines (27 loc) · 885 Bytes
/
main.cpp
File metadata and controls
32 lines (27 loc) · 885 Bytes
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
#include <iostream>
#include <string>
struct TrueValue
{
const std::string true_str;
};
struct FalseValue
{
const std::string false_str;
};
template<typename T>
bool string2bool(const std::string &str, T &&value)
{
constexpr bool is_true_exp = std::is_same_v<T, TrueValue>;
constexpr bool is_false_exp = std::is_same_v<T, FalseValue>;
if constexpr (is_true_exp) return str == value.true_str;
if constexpr (is_false_exp) return str != value.false_str;
static_assert(is_true_exp || is_false_exp, "'value' must be of either TrueValue or FalseValue types");
}
int main()
{
std::cout << string2bool("yes", TrueValue{"yes"}) << '\n';
std::cout << string2bool("no", TrueValue{"yes"}) << '\n';
std::cout << string2bool("yes", FalseValue{"no"}) << '\n';
std::cout << string2bool("no", FalseValue{"no"}) << '\n';
//std::cout << string2bool("no", "maybe") << '\n';
}