Skip to content

Commit 1d48137

Browse files
committed
Update examples with new literal types
1 parent 27bd980 commit 1d48137

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

doc/modules/ROOT/pages/literals.adoc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ The same asymmetry applies to `_i16`, `_i32`, `_i64`, and `_i128`.
143143

144144
== Examples
145145

146-
.This https://github.com/boostorg/safe_numbers/blob/develop/examples/literals.cpp[example] demonstrates how to use user-defined literals with safe integer types.
146+
.This https://github.com/boostorg/safe_numbers/blob/develop/examples/literals.cpp[example] demonstrates how to use user-defined literals with safe numeric types.
147147
====
148148
[source, c++]
149149
----
@@ -157,6 +157,13 @@ Output:
157157
100000_u32 = 100000
158158
9999999999_u64 = 9999999999
159159
max_u128 = 340282366920938463463374607431768211455
160+
-42_i8 = -42
161+
max_i16 = 32767
162+
min+1_i32 = -2147483647
163+
max_i64 = 9223372036854775807
164+
min+1_i128 = -170141183460469231731687303715884105727
165+
pi_f32 = 3.14
166+
e_f64 = 2.71828
160167
100_u32 + 50_u32 = 150
161168
6_u32 * 7_u32 = 42
162169
constexpr 255_u8 = 255

examples/literals.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// https://www.boost.org/LICENSE_1_0.txt
44

55
// This example demonstrates the use of user-defined literals for
6-
// constructing safe integer types. The literals provide a concise
6+
// constructing safe numeric types. The literals provide a concise
77
// syntax and perform compile-time range checking when possible.
88

99
#include <boost/safe_numbers/unsigned_integers.hpp>
10+
#include <boost/safe_numbers/signed_integers.hpp>
11+
#include <boost/safe_numbers/floats.hpp>
1012
#include <boost/safe_numbers/literals.hpp>
1113
#include <boost/safe_numbers/iostream.hpp>
1214
#include <iostream>
@@ -16,7 +18,7 @@ int main()
1618
using namespace boost::safe_numbers;
1719
using namespace boost::safe_numbers::literals;
1820

19-
// Construct safe integers using literal suffixes
21+
// Construct safe unsigned integers using literal suffixes
2022
{
2123
constexpr auto a {42_u8};
2224
constexpr auto b {1000_u16};
@@ -31,6 +33,32 @@ int main()
3133
std::cout << "max_u128 = " << e << std::endl;
3234
}
3335

36+
// Signed integer literals. Negative values are formed by applying unary
37+
// minus to a positive literal: -42_i32 parses as -(42_i32).
38+
{
39+
constexpr auto a {-42_i8};
40+
constexpr auto b {32767_i16};
41+
constexpr auto c {-2147483647_i32};
42+
constexpr auto d {9223372036854775807_i64};
43+
constexpr auto e {-170141183460469231731687303715884105727_i128};
44+
45+
std::cout << "-42_i8 = " << a << std::endl;
46+
std::cout << "max_i16 = " << b << std::endl;
47+
std::cout << "min+1_i32 = " << c << std::endl;
48+
std::cout << "max_i64 = " << d << std::endl;
49+
std::cout << "min+1_i128 = " << e << std::endl;
50+
}
51+
52+
// Floating-point literals. The literal accepts a long double and is
53+
// range-checked against the target type's maximum.
54+
{
55+
constexpr auto pi {3.14_f32};
56+
constexpr auto e {2.718281828459045_f64};
57+
58+
std::cout << "pi_f32 = " << pi << std::endl;
59+
std::cout << "e_f64 = " << e << std::endl;
60+
}
61+
3462
// Literals work naturally in expressions
3563
{
3664
const auto sum {100_u32 + 50_u32};
@@ -56,6 +84,8 @@ int main()
5684
//
5785
// auto bad = 256_u8; // throws std::overflow_error (> UINT8_MAX)
5886
// auto bad = 70000_u16; // throws std::overflow_error (> UINT16_MAX)
87+
// auto bad = 128_i8; // throws std::overflow_error (> INT8_MAX)
88+
// auto bad = 1.0e40_f32; // throws std::overflow_error (> FLT_MAX)
5989

6090
return 0;
6191
}

0 commit comments

Comments
 (0)