forked from bemanproject/optional
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd_vs_beman.cpp
43 lines (36 loc) · 1.55 KB
/
std_vs_beman.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
// examples/std_vs_beman.cpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <optional>
#include <beman/optional/optional.hpp>
#include <cassert>
#include <iostream>
int main() {
{
// Empty optional example.
std::optional<int> std_empty_opt;
beman::optional::optional<int> beman_empty_opt;
assert(!std_empty_opt.has_value() &&
!beman_empty_opt.has_value()); // or assert(!std_empty_opt && !beman_empty_opt);
std::cout << "std_vs_beman: .has_value() matches?: "
<< (std_empty_opt.has_value() == beman_empty_opt.has_value() ? "yes" : "no") << "\n";
}
{
// Optional with value example.
std::optional<int> std_opt = 26;
beman::optional::optional<int> beman_opt = 26;
assert(std_opt.has_value() && beman_opt.has_value()); // or assert(std_opt && beman_opt);
assert(std_opt.value() == beman_opt.value()); // or assert(*std_opt == *beman_opt);
std::cout << "std_vs_beman: .value() matches?: " << (std_opt.value() == beman_opt.value() ? "yes" : "no")
<< "\n";
}
// Note: std_empty_opt == beman_empty_opt and std_opt == beman_opt won't compile
// (no implementation of operator== for std::optional and beman::optional::optional).
return 0;
}
// # build example:
// $ cmake --workflow --preset gcc-14
//
// # run example:
// $ .build/gcc-14/examples/RelWithDebInfo/std_vs_beman
// std_vs_beman: .has_value() matches?: yes
// std_vs_beman: .value() matches?: yes