]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | (c) 2014 Glen Joseph Fernandes | |
3 | <glenjofe -at- gmail.com> | |
4 | ||
5 | Distributed under the Boost Software | |
6 | License, Version 1.0. | |
7 | http://boost.org/LICENSE_1_0.txt | |
8 | */ | |
9 | #ifndef MAKE_ALIGNED_HPP | |
10 | #define MAKE_ALIGNED_HPP | |
11 | ||
12 | #include "aligned_ptr.hpp" | |
13 | #include <boost/align/aligned_alloc.hpp> | |
14 | #include <boost/align/alignment_of.hpp> | |
15 | ||
16 | template<class T, class... Args> | |
17 | inline aligned_ptr<T> make_aligned(Args&&... args) | |
18 | { | |
19 | auto p = boost::alignment::aligned_alloc(boost:: | |
20 | alignment::alignment_of<T>::value, sizeof(T)); | |
21 | if (!p) { | |
22 | throw std::bad_alloc(); | |
23 | } | |
24 | try { | |
25 | auto q = ::new(p) T(std::forward<Args>(args)...); | |
26 | return aligned_ptr<T>(q); | |
27 | } catch (...) { | |
28 | boost::alignment::aligned_free(p); | |
29 | throw; | |
30 | } | |
31 | } | |
32 | ||
33 | #endif |