This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [c++0x] aligned_union implementation
- From: CoffeeBuzz <chris dot fairles at gmail dot com>
- To: libstdc++ at gcc dot gnu dot org
- Date: Tue, 28 Aug 2007 15:45:53 -0700 (PDT)
- Subject: Re: [c++0x] aligned_union implementation
- References: <12377647.post@talk.nabble.com>
CoffeeBuzz wrote:
>
> Tossed together a aligned_union implementation. I just want to make sure
> I'm understanding what its suppose to do.
>
> template<typename... _Types>
> struct __largest_alignment;
>
> template<typename _Tp, typename... _Types>
> struct __largest_alignment<_Tp,_Types...> {
> typedef typename conditional<
> (alignment_of<typename
> __largest_alignment<_Types...>::type>::value > alignment_of<_Tp>::value),
> integral_constant<size_t,alignment_of<typename
> __largest_alignment<_Types...>::type>::value>,
> integral_constant<size_t,alignment_of<_Tp>::value>>::type
> type;
> };
>
> template<typename _Tp>
> struct __largest_alignment<_Tp> {typedef
> integral_constant<size_t,alignment_of<_Tp>::value> type;};
>
> template<std::size_t _Len, typename... _Types>
> struct aligned_union {
> static const size_t alignment_value =
> __largest_alignment<_Types...>::type::value;
> typedef typename aligned_storage<_Len, alignment_value>::type type;
> };
>
> So 'alignment_value' gives the "largest" alignment of all the types. I'm
> not sure if "largest" means "most stringent" so correct me if I'm wrong.
> Now, for the type I think the above isn't quite correct since it must be
> "at least" _Len but if _Len is smaller than the largest in _Types then it
> should be sizeof(the largest type) correct?
>
> Chris
>
Actually, I just realized that you can use the ternary operator.
template<typename _Tp, typename... _Types>
struct __largest_alignment<_Tp,_Types...> {
typedef _Tp type;
static const size_t value =
alignment_of<typename __largest_alignment<_Types...>::type>::value >
alignment_of<_Tp>::value ?
alignment_of<typename __largest_alignment<_Types...>::type>::value :
alignment_of<_Tp>::value;
};
template<typename _Tp>
struct __largest_alignment<_Tp>
{ typedef _Tp type; static const size_t value =
alignment_of<_Tp>::value; };
And the aligned_union looks something like:
template<std::size_t _Len, typename... _Types>
struct aligned_union {
static const size_t alignment_value =
__largest_alignment<_Types...>::value;
typedef typename aligned_storage<(_Len > sizeof(typename
__largest_type<_Types...>::type) ? _Len : sizeof(typename
__largest_type<_Types...>::type)), alignment_value>::type type;
};
--
View this message in context: http://www.nabble.com/-c%2B%2B0x--aligned_union-implementation-tf4344680.html#a12377879
Sent from the gcc - libstdc++ mailing list archive at Nabble.com.