[PATCH] D27314: Replace AlignedCharArrayUnion maxed arity with a variadic template

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 1 12:55:34 PST 2016


Do we have MSVC support?
> On Dec 1, 2016, at 12:52 PM, David Blaikie <dblaikie at gmail.com> wrote:
> 
> Any chance we can just replace this all with http://en.cppreference.com/w/cpp/types/aligned_union <http://en.cppreference.com/w/cpp/types/aligned_union> ?
> 
> On Thu, Dec 1, 2016 at 11:56 AM Mehdi AMINI via Phabricator <reviews at reviews.llvm.org <mailto:reviews at reviews.llvm.org>> wrote:
> mehdi_amini created this revision.
> mehdi_amini added reviewers: zturner, dblaikie.
> mehdi_amini added a subscriber: llvm-commits.
> 
> This is NFC but for lifting the limitation to 10 types.
> 
> 
> https://reviews.llvm.org/D27314 <https://reviews.llvm.org/D27314>
> 
> Files:
>   llvm/include/llvm/Support/AlignOf.h
> 
> 
> Index: llvm/include/llvm/Support/AlignOf.h
> ===================================================================
> --- llvm/include/llvm/Support/AlignOf.h
> +++ llvm/include/llvm/Support/AlignOf.h
> @@ -30,44 +30,36 @@
>  };
> 
>  namespace detail {
> -template <typename T1,
> -          typename T2 = char, typename T3 = char, typename T4 = char,
> -          typename T5 = char, typename T6 = char, typename T7 = char,
> -          typename T8 = char, typename T9 = char, typename T10 = char>
> -class AlignerImpl {
> -  T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7; T8 t8; T9 t9; T10 t10;
> 
> -  AlignerImpl() = delete;
> -};
> +/// Compute the maximum alignment over the types in the variadic pack.
> +template <typename T0> constexpr size_t max_alignof() { return alignof(T0); }
> +template <typename T0, typename T1, typename... Ts>
> +constexpr size_t max_alignof() {
> +  return (max_alignof<T0>() > max_alignof<T1, Ts...>())
> +             ? max_alignof<T0>()
> +             : max_alignof<T1, Ts...>();
> +}
> +
> +/// Compute the maximum size over the types in the variadic pack.
> +template <typename T0> constexpr size_t max_sizeof() { return sizeof(T0); }
> +template <typename T0, typename T1, typename... Ts>
> +constexpr size_t max_sizeof() {
> +  return (max_sizeof<T0>() > max_sizeof<T1, Ts...>()) ? max_sizeof<T0>()
> +                                                      : max_sizeof<T1, Ts...>();
> +}
> 
> -template <typename T1,
> -          typename T2 = char, typename T3 = char, typename T4 = char,
> -          typename T5 = char, typename T6 = char, typename T7 = char,
> -          typename T8 = char, typename T9 = char, typename T10 = char>
> -union SizerImpl {
> -  char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
> -       arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
> -       arr9[sizeof(T9)], arr10[sizeof(T10)];
> -};
>  } // end namespace detail
> 
>  /// \brief This union template exposes a suitably aligned and sized character
> -/// array member which can hold elements of any of up to ten types.
> +/// array member which can hold any of the requested types.
>  ///
>  /// These types may be arrays, structs, or any other types. The goal is to
>  /// expose a char array buffer member which can be used as suitable storage for
> -/// a placement new of any of these types. Support for more than ten types can
> -/// be added at the cost of more boilerplate.
> -template <typename T1,
> -          typename T2 = char, typename T3 = char, typename T4 = char,
> -          typename T5 = char, typename T6 = char, typename T7 = char,
> -          typename T8 = char, typename T9 = char, typename T10 = char>
> -struct AlignedCharArrayUnion : llvm::AlignedCharArray<
> -    alignof(llvm::detail::AlignerImpl<T1, T2, T3, T4, T5,
> -                                      T6, T7, T8, T9, T10>),
> -    sizeof(::llvm::detail::SizerImpl<T1, T2, T3, T4, T5,
> -                                     T6, T7, T8, T9, T10>)> {
> -};
> +/// a placement new of any of these types.
> +template <typename... Ts>
> +struct AlignedCharArrayUnion
> +    : llvm::AlignedCharArray<detail::max_alignof<Ts...>(),
> +                             detail::max_sizeof<Ts...>()> {};
>  } // end namespace llvm
> 
>  #endif // LLVM_SUPPORT_ALIGNOF_H
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161201/f24d305f/attachment.html>


More information about the llvm-commits mailing list