3

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?

Drew
  • 12,578
  • 11
  • 58
  • 98
  • Your hunch seems to be correct. Related question (not dup) : https://stackoverflow.com/questions/42125733/mixing-types-and-nontypes-in-variadic-template-parameters , https://stackoverflow.com/questions/21101391/mixing-variadic-template-values-and-variadic-deduced-types . – javaLover Sep 07 '19 at 06:39

0 Answers0