LIPH's C++ Codes
no_destructor.h
Go to the documentation of this file.
1#ifndef LIPH_LANG_NO_DESTRUCTOR_H_
2#define LIPH_LANG_NO_DESTRUCTOR_H_
3
4#include <utility>
5
6namespace liph {
7
8template <class T>
10public:
11 template <typename... Args>
12 explicit no_destructor(Args&&...args) {
13 new (storage_) T(std::forward<Args>(args)...);
14 }
15
16 explicit no_destructor(const T& x) { new (storage_) T(x); }
17 explicit no_destructor(T&& x) { new (storage_) T(std::move(x)); }
18 no_destructor(const no_destructor&) = delete;
20 ~no_destructor() = default;
21
22 const T *get() const { return reinterpret_cast<const T *>(storage_); }
23 T *get() { return reinterpret_cast<T *>(storage_); }
24
25 const T& operator*() const { return *get(); }
26 T& operator*() { return *get(); }
27
28 const T *operator->() const { return get(); }
29 T *operator->() { return get(); }
30
31private:
32 char storage_[sizeof(T)];
33};
34
35} // namespace liph
36
37#endif // LIPH_LANG_NO_DESTRUCTOR_H_
Definition: no_destructor.h:9
T & operator*()
Definition: no_destructor.h:26
no_destructor(const T &x)
Definition: no_destructor.h:16
~no_destructor()=default
const T * operator->() const
Definition: no_destructor.h:28
const T & operator*() const
Definition: no_destructor.h:25
const T * get() const
Definition: no_destructor.h:22
no_destructor(const no_destructor &)=delete
no_destructor & operator=(const no_destructor &)=delete
T * operator->()
Definition: no_destructor.h:29
no_destructor(Args &&...args)
Definition: no_destructor.h:12
no_destructor(T &&x)
Definition: no_destructor.h:17
T * get()
Definition: no_destructor.h:23
Definition: algorithm.h:10