C++ Logo

std-proposals

Advanced search

[std-proposals] std::is_specialization_of

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sat, 10 Sep 2022 10:00:04 +0100
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:00:06