-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.h
37 lines (30 loc) · 891 Bytes
/
box.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
#include <memory>
template <typename T>
class box
{
std::unique_ptr<T> _impl;
public:
// Automatic construction from a `T`, not a `T*`.
box() : _impl(new T()) {}
box(T &&obj) : _impl(new T(std::move(obj))) {}
box(const T &obj) : _impl(new T(obj)) {}
// Copy constructor copies `T`.
box(const box &other) : box(*other._impl) {}
box &operator=(const box &other)
{
*_impl = *other._impl;
return *this;
}
// unique_ptr destroys `T` for us.
~box() = default;
// Access propagates constness.
T &operator*() { return *_impl; }
const T &operator*() const { return *_impl; }
T *operator->() { return _impl.get(); }
const T *operator->() const { return _impl.get(); }
};
template<class LHS, class RHS>
inline bool operator==(const box<LHS>& lhs, const box<RHS>& rhs){
return (*lhs)==(*rhs);
}