For those who want C++11 version without using fancy concepts or requires clause:
// Get the template arity for a given type C.
// MaxSize can be set by the user to avoid infinite search
template<template<typename...> class C, std::size_t MaxSize = 99>
struct template_arity;
// Get the default template arity for a given type C.
template<template<typename...> class C, std::size_t MaxSize = 99>
struct default_template_arity;
// Get mandatory template arity for a given type C
template<template<typename...> class C>
struct mandatory_template_arity;
Demo
https://godbolt.org/z/4n84r64eT
#include <functional>
#include <map>
#include <tuple>
#include <type_traits>
#include <vector>
template<typename...>
using void_t = void;
template<typename Tuple1, typename Tuple2> struct tuple_cat_type_impl;
template<typename... Ts, typename... Us> struct tuple_cat_type_impl<std::tuple<Ts...>, std::tuple<Us...>> { using type = std::tuple<Ts..., Us...>; };
template<typename Tuple1, typename Tuple2> using tuple_cat_t = typename tuple_cat_type_impl<Tuple1, Tuple2>::type;
template<typename T, std::size_t N> struct type_sequence_impl;
template<typename T> struct type_sequence_impl<T, 0> { using type = std::tuple<>; };
template<typename T> struct type_sequence_impl<T, 1> { using type = std::tuple<T>; };
template<typename T> struct type_sequence_impl<T, 2> { using type = std::tuple<T, T>; };
template<typename T, std::size_t N>
struct type_sequence_impl {
using type = tuple_cat_t<typename type_sequence_impl<T, (N / 2)>::type, typename type_sequence_impl<T, N - (N / 2)>::type>;
};
template<typename T, std::size_t N>
using type_sequence = typename type_sequence_impl<T, N>::type;
template<template<typename...> class C, typename ArgTuple, typename = void>
struct can_put_template : std::false_type {};
template<template<typename...> class C, typename... Args>
struct can_put_template<C, std::tuple<Args...>, void_t<C<Args...>>> : std::true_type {};
template<template<typename...> class C, std::size_t N, bool = can_put_template<C, type_sequence<int, N>>::value>
struct mandatory_template_arity_impl;
template<template<typename...> class C, std::size_t N> struct mandatory_template_arity_impl<C, N, true> : std::integral_constant<std::size_t, N> {};
template<template<typename...> class C, std::size_t N> struct mandatory_template_arity_impl<C, N, false> : mandatory_template_arity_impl<C, N + 1> {};
template<template<typename...> class C>
struct mandatory_template_arity : mandatory_template_arity_impl<C, 0> {};
template<template<typename...> class C, std::size_t Size, bool = can_put_template<C, type_sequence<int, Size>>::value /* true */>
struct template_arity_impl;
template<template<typename...> class C, std::size_t Size> struct template_arity_impl<C, Size, true> : std::integral_constant<std::size_t, Size> {};
template<template<typename...> class C, std::size_t Size> struct template_arity_impl<C, Size, false> : template_arity_impl<C, Size - 1> {};
template<template<typename...> class C> struct template_arity_impl<C, 0, false> : std::integral_constant<std::size_t, std::size_t(-1)> {};
template<template<typename...> class C, std::size_t MaxSize = 6>
struct template_arity : template_arity_impl<C, MaxSize>{};
template<template<typename...> class C, std::size_t MaxSize = 6>
struct default_template_arity
: std::integral_constant<std::size_t, template_arity<C>::value - mandatory_template_arity<C>::value> {};
template<typename = void>
struct cxx14_less {};
int main() {
static_assert(template_arity<std::vector>::value == 2, "");
static_assert(mandatory_template_arity<std::vector>::value == 1, "");
static_assert(template_arity<std::map>::value == 4, "");
static_assert(mandatory_template_arity<std::map>::value == 2, "");
static_assert(template_arity<cxx14_less>::value == 1, "");
static_assert(mandatory_template_arity<cxx14_less>::value == 0, "");
return 0;
}
template_count
expected to do withtemplate<typename = void> struct test{};
andtemplate<typename...> struct test{};
? – Paigepaikreturn 3;
; I understand you may have reduced a different problem to if I have a solution to this problem, but what I'm asking for is the problem you reduced to this. – Therein