我有以下类型帮助器来检测类型是否是模板的实例:
#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>>);
}
在大多数情况下,这可以正常工作,除非模板包含的参数不是类型。我试图将模板模板参数模板参数更改为auto ...,但是它将不接受模板参数包中的类型。当传入的模板中可能有非类型参数与类型参数混合在一起时,是否有办法检测类型T是否为ANY模板的实例。