Skip to content

Commit 31ce48d

Browse files
Merge pull request #3 from contour-terminal/improvement/cast_rval
Add rvalue operator. Update of test and documentation.
2 parents c54a288 + 39e19b5 commit 31ce48d

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This header can be simply copied into a project or used via CMake builtin functi
1111

1212
# Simple usage
1313

14+
Example of creation boxed structures and usage
1415
``` c++
1516

1617
#include <boxed-cpp/boxed.hpp>
@@ -39,6 +40,28 @@ int main()
3940

4041
```
4142

43+
44+
When you need to get value from boxed type, you need to unbox it
45+
``` c++
46+
//unbox in declared type. double in this case
47+
auto speed_value_native = unbox(speed_of_light);
48+
//unbox into float type
49+
auto speed_value_float = unbox<float>(speed_of_light);
50+
// unbox into int type
51+
auto speed_value_int = unbox<int>(speed_of_light);
52+
```
53+
54+
55+
You can also evaluate expressions with boxed types without the need of unboxing them if explicitly declare the resulted type
56+
``` c++
57+
auto speed_of_light = Speed(299792458.0);
58+
auto value = speed_of_light * 2.0; // type of value is Speed
59+
60+
// boxed value will be automatically unboxed into type that was boxed, in this case double
61+
double value_d = speed_of_light * 2.0;
62+
```
63+
64+
4265
# More advanced usage
4366
You can forget about the order of parameters in your code
4467

include/boxed-cpp/boxed.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct boxed
6868

6969
[[nodiscard]] constexpr T& get() noexcept { return value; }
7070
[[nodiscard]] constexpr T const& get() const noexcept { return value; }
71+
constexpr operator T() const && {return value;}
7172

7273
template <typename To>
7374
[[nodiscard]] constexpr auto as() const noexcept

test-boxed-cpp.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <boxed-cpp/boxed.hpp>
1616
#include <catch2/catch.hpp>
1717
#include <limits>
18+
#include <type_traits>
1819

1920
namespace tags { struct Length{}; struct From{}; struct To{}; }
2021

@@ -80,3 +81,32 @@ TEST_CASE("function with boxed variables")
8081
auto value = std::abs(unbox( wave_speed(vacuum_permittivity, vacuum_permeability) - speed_of_light ));
8182
REQUIRE(value < std::numeric_limits<double>::epsilon());
8283
}
84+
85+
TEST_CASE("unbox types check")
86+
{
87+
auto speed_of_light = Speed(299792458.0);
88+
auto speed_value_native = unbox(speed_of_light);
89+
static_assert(std::is_same_v<decltype(speed_value_native),double>);
90+
auto speed_value_float = unbox<float>(speed_of_light);
91+
static_assert(std::is_same_v<decltype(speed_value_float),float>);
92+
auto speed_value_int = unbox<int>(speed_of_light);
93+
static_assert(std::is_same_v<decltype(speed_value_int),int>);
94+
}
95+
96+
97+
TEST_CASE("unbox without template parameters to initial type")
98+
{
99+
auto speed_of_light = Speed(299792458.0);
100+
REQUIRE( std::abs(unbox(speed_of_light) - 299792458.0) < std::numeric_limits<double>::epsilon());
101+
}
102+
103+
TEST_CASE("cast inside rvalue")
104+
{
105+
auto speed_of_light = Speed(299792458.0);
106+
107+
auto distance_auto = speed_of_light * 2.0;
108+
static_assert(std::is_same_v<decltype(distance_auto),Speed>);
109+
110+
double distance_d = speed_of_light * 2.0;
111+
REQUIRE(distance_d - 2.0 * 299792458.0 < std::numeric_limits<double>::epsilon());
112+
}

0 commit comments

Comments
 (0)