Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
template <class T, template<class, class...> class Container>
struct A {
typedef Container<T> type;
};
template <typename T, template<typename, typename> class Container>
struct A
{
typedef Container<T, std::allocator<T> > type;
};
template <typename T, template<typename Y, typename = std::allocator<Y> > class Container>
struct A
{
typedef Container<T> type;
};
Мы должны заглянуть в реализацию std::vector, выяснить, какой тип аллокатора используется по умолчанию, и тогда использовать его.
std контейнеры используют std::allocator.template <typename T, typename A = std::allocator<T>, typename _P1 = def1, typename _P2 = def2, ...>
class vector;
— вполне может быть валидной сигнатурой vector.std::vector и т. д. — вполне стандартизированные вещи.template <typename T, typename A = std::allocator<T>, typename _P1 = def1, typename _P2 = def2, ...>
class vector;
#include <vector>
#include <map>
#include <array>
template <typename C, typename = decltype(
std::begin(std::declval<C>()), void(),
*std::begin(std::declval<C>()), void(),
std::end(std::declval<C>()), void(),
std::next(std::begin(std::declval<C>())), void())>
struct BetterA
{
typedef decltype(*std::begin(std::declval<const C>())) type;
};
void test()
{
auto test1 = BetterA<std::vector<int>>();
auto test2 = BetterA<std::map<int, int>>();
auto test3 = BetterA<int[10]>();
auto test4 = BetterA<std::array<int, 10>>();
}
BetterA<int[10]>.template <typename Cont>
struct A
{
typedef typename Cont::value_type type;
};
template <typename T, size_t N>
struct A<T[N]>
{
typedef T type;
};
void test()
{
auto test1 = A<std::vector<int>>::type();
auto test2 = A<std::map<int, int>>::type();
auto test3 = A<int[10]>::type();
auto test4 = A<std::array<int, 10>>::type();
}
int*, естественно, компилятор не съест.std::declval возвращает r-value ссылку на значение, в то время как std::begin принимает массив только по обычной ссылке, поэтому конструкцияstd::begin(std::declval<int[10]>())
не компилируется.template <typename T>
const T& declval2();
template <typename C>
struct BetterA
{
typedef typename std::decay<decltype(*std::begin(declval2<const C>()))>::type type;
};
но мой изначальный вариант мне нравится больше.
Отказываемся от шаблонных шаблонных параметров