On Thu, Feb 20, 2014 at 1:21 PM, Glen Fernandes
Can this class be extended so that I can specify the alignment explicitly in the template parameters?
Definitely. Instead of specifying the alignment value as a template parameter, maybe a preferable design is:
template
class aligned_allocator; ...where 'A' could supply different alignment values for different rebound types.
I think it complicates things too much. For example, if I just want to align memory at cache line boundary (64 bytes for Intel CPUs), I would have to create a metafunction just to provide this single constant to the allocator. I can understand why you would want to use a metafunction here - you want rebound allocator to use the proper alignment. But realistically, no one ever wants to align memory weaker than malloc/new, and it is already guaranteed to align properly for any builtin types. So you would specify a stronger alignment than malloc/new provides, which means it should already be strong enough for any T. That's why I think a simple constant would be enough. template< typename T, unsigned int Alignment = 0 > class aligned_allocator { static constexpr unsigned int alignment = Alignment > 0 ? Alignment : alignment_of< T >::value; static_assert(alignment >= alignment_of< T >::value, "Specified alignment is too weak"); }; But if you decide to go with the metafunction, then at least provide a boilerplate so that it is easy to specify a constant.