C++ Logo


Advanced search

Re: [std-proposals] std::is_specialization_of

From: Jiang An <de34_at_[hidden]>
Date: Sat, 10 Sep 2022 17:04:22 +0800

Jiang An

在 Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]>,2022年9月10日 17:00写道:

If you can see my previous post on this mailing list about "common_base" for "std::variant", you'll see how I check at compile-time if a concrete class is a specialisation of a given template class.

Well there should be "std::is_specialization_of" in the C++ Standard as follows:

        #include <type_traits>

        template<typename Test, template<typename...> class Ref>
        struct is_specialization_of : std::false_type {};

        template<template<typename...> class Ref, typename... Args>
        struct is_specialization_of<Ref<Args...>, Ref>: std::true_type {};

Also there should be:

        template<typename Test, template<typename...> class Ref>
        inline constexpr bool is_specialization_of_v = is_specialization_of<Test,Ref>::value;

And here you can see it in action:

    #include <iostream>
    #include <vector>
    #include <list>

    using namespace std;
    int main(void)
        if constexpr ( is_specialization_of_v< vector<int>, vector>) cout << "vector\n";

        if constexpr ( is_specialization_of_v< vector<int>, list>) cout << "list\n";

Received on 2022-09-10 09:04:33