On Nov 11, 2:13*pm, abir <abirba...@gmail.comwrote:
On Nov 11, 4:59 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Nov 11, 6:47 am, abir <abirba...@gmail.comwrote:
*In my program i wan to use a certain type as both type T1 (usually
non-pod)
* * and also T2 (usually size_t)
* * so an union like this doesn't work
* * template<typename T>
union utype{
* * T v_;
* * std::size_t sz_;};
utype* memory_;
where T is
struct nonpod{
* * int x;
* * int y;
* * nonpod(){}
};
I am not familiar to aligned_storage or its usage.
Can i make something like this?
[]
You could.
Or you can take a ready-made solution: boost::variant<>.
Probably i could, but i am more interested to know how that ready-
made
solution is made.
And i am rather interested to know the usage of aligned_storage &
alignment_of
Not finding enough documentation on them.
You use aligned_storage<>::type as a member or a stack variable, so
that the compiler aligns it properly.
Using malloc(N * sizeof(aligned_storage<>::type)) does not guarantee
alignment in the general case. malloc() returns memory suitably
aligned for a built-in type with the largest alignment requirement
(which often is double or long double). Thus, if the alignment
requirement for aligned_storage<>::type is greater than that of a
built-in type with the largest alignment requirement, malloc() is not
directly suitable (you need to allocate more memory with malloc() and
align it manually). On the other hand, if the alignment requirement
for aligned_storage<>::type is equal to or less than that of a built-
in type with the largest alignment requirement, than you don't need
aligned_storage<>::type when using malloc(), just use the largest size
malloc(N * std::max(sizeof(nonpod), sizeof(size_t))).
so whether the alignment_type below going to be aligned for both
value_type & size_type
typedef typename std::tr1::aligned_storage<sizeof(value_type)
* * * * ,std::tr1::alignment_of<size_type>::value>::type
aligned_type;
aligned_type here is only guaranteed to have alignment size_type and
the size of value_type.
and i am interested to have a loki style not-so-small-object allocator
(at least not smaller than size_type)
and each storage can hold more than 255 objects & guided by size_type.
I depends whether that allocator needs to support types with alignment
requirement greater than that of the built-in type with the largest
alignment requirement (align on processor cache size for example). If
it does not need to (like most allocators), than malloc() is fine and
you don't need to use aligned_storage<>::type. If it does, you don't
need aligned_storage<>::type either, because you are going to use
functions like mmap, posix_memalign() to allocate suitably aligned
memory, or manually align memory allocated with malloc().
--
Max