-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpimpl_library.cpp
More file actions
34 lines (25 loc) · 1.01 KB
/
pimpl_library.cpp
File metadata and controls
34 lines (25 loc) · 1.01 KB
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
#include "pimpl_library.h"
#include <utility>
// -----------------------------------------------------------------------------
// PIMPL LIBRARY
// -----------------------------------------------------------------------------
template <typename T> pimpl<T>::pimpl() : impl{new T{}} {}
template <typename T> pimpl<T>::~pimpl() {
// destruction is handled by unique_ptr
}
template <typename T>
template <typename... Args>
pimpl<T>::pimpl(Args &&... args) : impl{new T{std::forward<Args>(args)...}} {}
template <typename T> T *pimpl<T>::operator->() { return impl.get(); }
template <typename T> T &pimpl<T>::operator*() { return *impl.get(); }
// -----------------------------------------------------------------------------
// USAGE
// -----------------------------------------------------------------------------
// in .h
class Foo {
class Impl;
pimpl<Impl> impl; // This provides all details we need, We just need to
// implement the FooImpl class.
};
// keep Foo::Impl in .cpp.
class Foo::Impl {};