MCPcopy Create free account
hub / github.com/pybind/pybind11 / ReferenceSensitiveOptional

Class ReferenceSensitiveOptional

tests/test_stl.cpp:124–162  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

122// See issue #3330 for more details.
123template <typename T>
124class ReferenceSensitiveOptional {
125public:
126 using value_type = T;
127
128 ReferenceSensitiveOptional() = default;
129 // NOLINTNEXTLINE(google-explicit-constructor)
130 ReferenceSensitiveOptional(const T &value) : storage{value} {}
131 // NOLINTNEXTLINE(google-explicit-constructor)
132 ReferenceSensitiveOptional(T &&value) : storage{std::move(value)} {}
133 ReferenceSensitiveOptional &operator=(const T &value) {
134 storage = {value};
135 return *this;
136 }
137 ReferenceSensitiveOptional &operator=(T &&value) {
138 storage = {std::move(value)};
139 return *this;
140 }
141
142 template <typename... Args>
143 T &emplace(Args &&...args) {
144 storage.clear();
145 storage.emplace_back(std::forward<Args>(args)...);
146 return storage.back();
147 }
148
149 const T &value() const noexcept {
150 assert(!storage.empty());
151 return storage[0];
152 }
153
154 const T &operator*() const noexcept { return value(); }
155
156 const T *operator->() const noexcept { return &value(); }
157
158 explicit operator bool() const noexcept { return !storage.empty(); }
159
160private:
161 std::vector<T> storage;
162};
163
164namespace PYBIND11_NAMESPACE {
165namespace detail {

Callers

nothing calls this directly

Calls 5

moveFunction · 0.85
valueFunction · 0.85
emplace_backMethod · 0.80
clearMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected