template<
typename T>
class utils::Box< T >
Remote storage for a single item. Implemented as a unique pointer that is never null
, except when moved from.
Has the semantics of non-optional T
. Copies the content on copy, compares by the contained value.
Use in the following cases:
- to create recursive types while maintaining value semantics;
- to hide the implementation of a class in cpp;
- to prevent the large size or alignment of a field from inflating the size or alignment of an object.
Use utils::UniqueRef instead:
- to add a non-movable field to a movable object;
- to own an object of a polymorphic base class.
Usage example:
struct RecursionStarterPack {
std::string structs;
std::string strings;
};
struct PackSection {
std::string box;
std::optional<RecursionStarterPack> here_we_go_again;
};
TEST(UtilsBox, Sample) {
PackSection pack{
"box",
{{
"structs",
"strings",
"box",
std::nullopt,
}},
}},
};
auto another_pack = pack;
EXPECT_EQ(another_pack, pack);
another_pack.here_we_go_again->structs = "modified structs";
EXPECT_NE(another_pack, pack);
EXPECT_EQ(pack.here_we_go_again->structs, "structs");
}
Definition at line 56 of file box.hpp.
|
| Box () |
| Allocate a default-constructed value.
|
|
template<typename U = T, std::enable_if_t< impl::ConjunctionWithTrait< impl::kArgsAreNotSelf< Box, U >, std::is_convertible, U &&, T >(), int > = 0> |
| Box (U &&arg) |
| Allocate a T , copying or moving arg.
|
|
template<typename... Args, std::enable_if_t< impl::ConjunctionWithTrait< impl::kArgsAreNotSelf< Box, Args... >, std::is_constructible, T, Args &&... >(), int > = 0> |
| Box (Args &&... args) |
| Allocate the value, emplacing it with the given args.
|
|
| Box (Box &&other) noexcept=default |
|
Box & | operator= (Box &&other) noexcept=default |
|
| Box (const Box &other) |
|
Box & | operator= (const Box &other) |
|
template<typename U = T, std::enable_if_t< impl::ConjunctionWithTrait< impl::ConjunctionWithTrait< impl::kArgsAreNotSelf< Box, U >, std::is_constructible, T, U >(), std::is_assignable, T &, U >(), int > = 0> |
Box & | operator= (U &&other) |
| Assigns-through to the contained value.
|
|
| operator bool () const =delete |
|
T * | operator-> () noexcept |
|
const T * | operator-> () const noexcept |
|
T & | operator* () noexcept |
|
const T & | operator* () const noexcept |
|
bool | operator== (const Box &other) const |
|
bool | operator!= (const Box &other) const |
|
bool | operator< (const Box &other) const |
|
bool | operator> (const Box &other) const |
|
bool | operator<= (const Box &other) const |
|
bool | operator>= (const Box &other) const |
|