atomgalaxy / isocpp-universal-template-param

We propose a way to spell a universal template parameter kind. This would allow for a generic apply and other higher-order template metafunctions, and certain typetraits.
https://atomgalaxy.github.io/isocpp-universal-template-param/d1985r0.pdf
2 stars 2 forks source link

Allowing setting a UA to a template created on the spot. #49

Open BengtGustafsson opened 1 year ago

BengtGustafsson commented 1 year ago

I wrote this text for P2667 which introduces buffered_allocator<T, SZ, Backing> where Backing is another allocator. For flat_maps I got this idea:

Another option, specific for flat_map and flatmultimap, would be to let them take a template template parameter for the allocator and use specializations of this template for Key and T alike. This reduces flexibility a bit but simplifies flat*map with custom allocators in general.

// Modified flat_map definition:
template<typename Key, typename T, typename Compare = less<Key>, 
         template<typename T> class Alloc = std::allocator> 
class flat_map;

To use this with a buffered_allocator with size we need a helper type alias to bind the size and Backing allocator, but leave the type unbound. This can't be done without P1985 if flat_map expects to be able to specialize its template template parameter like Alloc<Key> andAlloc<T>. Even with P1985 this can't be done without a helper struct template.

template<size_t SZ, typename Backing> struct bound_allocator_helper {
    template<typename T> using tpl = buffered_allocator<T, SZ, Backing>;
};

template<size_t SZ, typename Backing>
__any bound_allocator = bound_allocator_helper<SZ, Backing>::tpl;

Maybe this can be written even more concisely in the future, but assigning an on the spot created type alias template to a universal alias template seems scary.

template<size_t SZ, typename Backing>
__any bound_allocator = template<typename T> using buffered_allocator<T, SZ, Backing>;

Well there is a task for a new TMP-library based on UTP/UA. To be able to produce a type alias that flat_map can use requires binding the SZ and Backing parameters of buffered_allocator from template parameters... how hard can it be?