1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or utilied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #ifndef REALM_OS_ALIGNED_UNION_HPP
20 #define REALM_OS_ALIGNED_UNION_HPP
23 #include <initializer_list>
24 #include <type_traits>
28 // Provide our own implementation of max as GCC 4.9's is not marked as constexpr.
32 static constexpr const T& constexpr_max(const T& a, const T& b)
38 static constexpr const T& constexpr_max(const T* begin, const T *end)
40 return begin + 1 == end ? *begin : constexpr_max(*begin, constexpr_max(begin + 1, end));
44 static constexpr const T& constexpr_max(std::initializer_list<T> list)
46 return constexpr_max(list.begin(), list.end());
53 // Provide our own implementation of `std::aligned_union` as it is missing from GCC 4.9.
54 template <size_t Len, typename... Types>
57 static constexpr size_t alignment_value = _impl::constexpr_max({alignof(Types)...});
58 static constexpr size_t storage_size = _impl::constexpr_max({Len, sizeof(Types)...});
59 using type = typename std::aligned_storage<storage_size, alignment_value>::type;
65 #endif // REALM_OS_ALIGNED_UNION_HPP