C/C++ API Reference
Loading...
Searching...
No Matches
no_destructor.h
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <type_traits>
17#include <utility>
18
19#include "pw_polyfill/language_feature_macros.h"
20
21namespace pw {
22
24
76template <typename T>
78 public:
79 using value_type = T;
80
81 // Initializes a T in place.
82 //
83 // This overload is disabled when it might collide with copy/move.
84 template <typename... Args,
85 typename std::enable_if<!std::is_same<void(std::decay_t<Args>&...),
86 void(NoDestructor&)>::value,
87 int>::type = 0>
88 explicit constexpr NoDestructor(Args&&... args)
89 : storage_(std::forward<Args>(args)...) {}
90
91 // Move or copy from the contained type. This allows for construction from an
92 // initializer list, e.g. for std::vector.
93 explicit constexpr NoDestructor(const T& x) : storage_(x) {}
94 explicit constexpr NoDestructor(T&& x) : storage_(std::move(x)) {}
95
96 NoDestructor(const NoDestructor&) = delete;
97 NoDestructor& operator=(const NoDestructor&) = delete;
98
99 ~NoDestructor() = default;
100
101 constexpr const T& operator*() const { return get(); }
102 constexpr T& operator*() { return get(); }
103
104 constexpr const T* operator->() const { return &get(); }
105 constexpr T* operator->() { return &get(); }
106
107 private:
108 constexpr T& get() {
109 if constexpr (std::is_trivially_destructible_v<T>) {
110 return storage_;
111 } else {
112 return storage_.value;
113 }
114 }
115
116 constexpr const T& get() const {
117 if constexpr (std::is_trivially_destructible_v<T>) {
118 return storage_;
119 } else {
120 return storage_.value;
121 }
122 }
123
124 union NonTrivialStorage {
125 template <typename... Args>
126 constexpr NonTrivialStorage(Args&&... args)
127 : value(std::forward<Args>(args)...) {}
128
129 // Unfortunately, this cannot be trivially destructible because having a
130 // union member of non-trivially destructible T implicitly deletes the
131 // destructor. Trivial destruction may be possible in future C++ standards.
132 PW_CONSTEXPR_CPP20 ~NonTrivialStorage() {}
133
134 T value;
135 };
136
137 std::conditional_t<std::is_trivially_destructible_v<T>, T, NonTrivialStorage>
138 storage_;
139};
140
141} // namespace pw
Definition: no_destructor.h:77
#define PW_CONSTEXPR_CPP20
Definition: language_feature_macros.h:27
The Pigweed namespace.
Definition: alignment.h:27