I'm trying to create a type trait that detects of a type if an instantiation of a given template. For example:
static_assert(IsInstantiationOf<std::vector, std::vector<int>>);
static_assert(IsInstantiationOf<std::map, std::map<int, bool>>);
static_assert(!IsInstantiationOf<std::vector, std::map<int, bool>>);
I have this working for many types, but not for non-type template arguments:
#include <type_traits>
#include <vector>
#include <map>
#include <array>
template<template <typename ...> typename Of, typename ...T>
struct IsInstatiationOf : public std::false_type {};
template<template <typename ...> typename Of, typename ...T>
struct IsInstatiationOf<Of, Of<T...>> : public std::true_type {};
bool my_less(int a, int b) {
return false;
}
static_assert(IsInstatiationOf<std::vector, std::vector<int>>());
static_assert(IsInstatiationOf<std::map, std::map<int, int, decltype(my_less)>>());
static_assert(!IsInstatiationOf<std::map, std::vector<int>>());
static_assert(IsInstatiationOf<std::array, std::array<int, 4>>()); // error :(
On godbolt: https://godbolt.org/z/2k4syB
My hunch is that this can't be done because you can't have a parameter pack of mixed types and values. Is that correct? Or is there some way to do this that I'm missing?