Declare a container object from a template template and variable templates

I need to declare a class that can store various types of containers. Those. it would be nice if it could handle std :: bitset and std :: array. However, these two classes need different template arguments ... Is it possible (and possibly how) to use template template classes and variable templates to declare this class?

Example (but incorrect):

template<template <typename..., std::size_t> class Container, std::size_t N, typename... Args> class Base_Class { ... Container<Args..., N/2> container; }; 

The compiler complains that N / 2 is not a type. Obviously, for std :: array and std :: bitset I need a size that will be the last parameter of the template ... Is it possible to encode this crazy one?

Thanks!

EDIT: As far as I know, the main problem is that the variation patterns can only be expanded on the right, so the variable parameter should be the last. Does anyone know if there are plans to allow the following syntax in C ++ 17?

 template<typename... Args, typename T> struct A {}; 
+5
source share
2 answers

Anton’s answer can be made somewhat less container specific by using the template template options for the ResizedContainer specifications:

 namespace detail { template<typename Container> struct ResizedContainer; template<template<typename,std::size_t> class Container, typename T, std::size_t N> struct ResizedContainer<Container<T,N>> { using type = Container<T,N/2>; }; template<template<std::size_t> class Container, std::size_t N> struct ResizedContainer<Container<N>> { using type = Container<N/2>; }; } #include <array> #include <bitset> template<typename Container> class Base_Class { typename detail::ResizedContainer<Container>::type container; }; int main() { Base_Class<std::array<int,4>> a; Base_Class<std::bitset<5>> b; } 
+3
source

Maybe something like this:

 namespace detail { template<typename Container> struct ResizedContainer; template<typename T, size_t N> struct ResizedContainer<std::array<T, N>> { using type = std::array<T, N/2>; }; template<size_t N> struct ResizedContainer<std::bitset<N>> { using type = std::bitset<N/2>; }; } template<typename Container> class Base_Class { typename detail::ResizedContainer<Container>::type container; }; int main() { Base_Class<std::array<int, 4>> a; Base_Class<std::bitset<5>> b; } 
+2
source

All Articles