Skip to content

Instantly share code, notes, and snippets.

@insooth
Last active June 17, 2016 08:58
Show Gist options
  • Save insooth/9820b7ea2ba0b67b6be3c807c04f3c3c to your computer and use it in GitHub Desktop.
Save insooth/9820b7ea2ba0b67b6be3c807c04f3c3c to your computer and use it in GitHub Desktop.
// Having type T checks whether it is an instance of template U<V>.
// http://coliru.stacked-crooked.com/a/b5bcde32c99c7cab
#include <vector>
#include <type_traits>
template<class T, template<typename...> class U, class V = void>
struct is_instance_of : std::false_type
{};
template<template<typename...> class U, class... Args>
struct is_instance_of<U<Args...>, U, void> : std::true_type
{};
#if !defined( __GNUC__ )
template<class V, template<V...> class U, V... Args>
struct is_instance_of<U<Args...>, U, V> : std::true_type
{};
#endif
template<class V, class T, template<V...> class U>
struct is_integral_instance_of : std::false_type
{};
template<class V, template<V...> class U, V... i>
struct is_integral_instance_of<V, U<i...>, U> : std::true_type
{};
template<int... i>
struct S{};
int main()
{
using T = std::vector<int>;
using U = std::vector<int>;
using V = S<10, 11, 12>;
static_assert(is_instance_of<T, std::vector>::value, "NOK");
static_assert(is_instance_of<U, std::vector>::value, "NOK");
static_assert(is_integral_instance_of<int, V, S>::value, "NOK");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment