• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[/
2Copyright 2018 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed under the Boost Software License, Version 1.0.
6(http://www.boost.org/LICENSE_1_0.txt)
7]
8
9[section:empty_value empty_value]
10
11[simplesect Authors]
12
13* Glen Fernandes
14
15[endsimplesect]
16
17[section Overview]
18
19The header <boost/core/empty_value.hpp> provides the class template
20`boost::empty_value` for library authors to conveniently leverage the Empty
21Base Optimization to store objects of potentially empty types.
22
23[endsect]
24
25[section Examples]
26
27The following example shows `boost::empty_value` used to create a type that
28stores a pointer, comparer, and allocator, where the comparer and allocator
29could be empty types.
30
31```
32template<class Ptr, class Compare, class Allocator>
33class storage
34    : empty_value<Compare, 0>
35    , empty_value<Allocator, 1> {
36public:
37    storage()
38        : empty_value<Compare, 0>(empty_init_t())
39        , empty_value<Allocator, 1>(empty_init_t())
40        , ptr_() { }
41
42    storage(const Compare& c, const Allocator& a)
43        : empty_value<Compare, 0>(empty_init_t(), c)
44        , empty_value<Allocator, 1>(empty_init_t(), a)
45        , ptr_() { }
46
47    const Ptr& pointer() const {
48        return ptr_;
49    }
50
51    Ptr& pointer() {
52        return ptr_;
53    }
54
55    const Compare& compare() const {
56        return empty_value<Compare, 0>::get();
57    }
58
59    Compare& compare() {
60        return empty_value<Compare, 0>::get();
61    }
62
63    const Allocator& allocator() const {
64        return empty_value<Allocator, 1>::get();
65    }
66
67    Allocator& allocator() {
68        return empty_value<Allocator, 1>::get();
69    }
70
71private:
72    Ptr ptr_;
73};
74```
75
76[endsect]
77
78[section Reference]
79
80```
81namespace boost {
82
83struct empty_init_t { };
84
85template<class T, unsigned Index = 0, bool Empty = ``/see below/``>
86class empty_value {
87public:
88    typedef T type;
89
90    empty_value() = default;
91
92    template<class... Args>
93    empty_value(empty_init_t, Args&&... args);
94
95    const T& get() const noexcept;
96
97    T& get() noexcept;
98};
99
100inline constexpr empty_init_t empty_init{ };
101
102} /* boost */
103```
104
105[section Template parameters]
106
107[variablelist
108[[`T`][The type of value to store]]
109[[`Index`][Optional: Specify to create a distinct base type]]
110[[`Empty`][Optional: Specify to force inheritance from type]]]
111
112[endsect]
113
114[section Member types]
115
116[variablelist
117[[`type`][The template parameter `T`]]]
118
119[endsect]
120
121[section Constructors]
122
123[variablelist
124[[`empty_value() = default;`][Default initialize the value]]
125[[`template<class... Args> empty_value(empty_init_t, Args&&... args);`]
126[Initialize the value with `std::forward<Args>(args)...`]]]
127
128[endsect]
129
130[section Member functions]
131
132[variablelist
133[[`const T& get() const noexcept;`][Returns the value]]
134[[`T& get() noexcept;`][Returns the value]]]
135
136[endsect]
137
138[endsect]
139
140[endsect]
141