0

I have the following type helper to detect whether a type is an instance of a template:

#include <type_traits>

namespace detail
{
    template<typename T, template <typename...> typename Template>
    struct IsInstanceOf : public std::false_type {};

    template<typename... T, template<typename...> typename Template>
    struct IsInstanceOf<Template<T...>, Template> : public std::true_type {};
}

template<template <typename...> typename Template, typename T>
constexpr bool is_instance_of = detail::IsInstanceOf<T, Template>::value;

template<typename A>
struct Foo{};

template<typename A, std::size_t B>
struct Bar{};

int main()
{
    static_assert(is_instance_of<Foo, Foo<float>>);
    //static_assert(is_instance_of<Bar, Bar<float, 1>>);
}

This is working fine in most cases, except when the template contains a parameter that is not a type. I tried to change the template template parameters template arguments to auto..., but then it will not accept types in the template's parameter pack. Is there a way to detect whether type T is an instance of ANY template when the templates passed in might have a non type parameter mixed in with type parameters.

Andreas Loanjoe
  • 2,205
  • 10
  • 26
  • 1
    Unfortunately type and non type templates don't mix so well for this. I've looked into this before but I never found a solution I liked. – NathanOliver Nov 15 '19 at 16:48
  • You could try to wrap the `size_t` in an `std::integral_constant`, but that would of course mean to change you class to something uglier. – n314159 Nov 15 '19 at 16:50

0 Answers0