C++ Logo

std-proposals

Advanced search

Re: [std-proposals] is_specialization_of

From: Ivan Lazaric <ivan.lazaric1_at_[hidden]>
Date: Sat, 17 Jan 2026 23:10:58 +0100
You can do this with reflection now:
https://godbolt.org/z/o6vhTYrxn

On Sat, Jan 17, 2026 at 10:10 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:

> I posted two years ago about the progress of bringing
> 'is_specialization_of' into the Standard:
>
> https://lists.isocpp.org/std-proposals/2024/03/9445.php
>
> The problem is that the following definition:
>
> namespace std {
> template<typename _Tp, template<typename...> class _Class>
> constexpr bool __is_specialization_of = false;
> template<template<typename...> class _Class, typename... _Args>
> constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
> true;
> }
>
> won't work for template classes that have numbers for parameters, such
> as std::array.
>
> Some people say that we can't have 'is_specialization_of' in the
> Standard until we have something like:
>
> template<typename_or_constant...> class Class;
>
> But I want to argue that we don't need 'typename_or_constant'. Instead
> we just need the Standard to say that 'is_specialization_of' requires
> compiler support as follows:
>
> namespace std {
> template< class T , /* COMPILER_SUPPORT */ Primary >
> constexpr bool is_specialization_of = . . . ;
> }
>
> Yesterday I finished implementing 'is_specialization_of' in the GNU
> g++ compiler. The first thing I did was make a new built-in function
> called "__is_specialization_of". After I got this working, I had to
> define the standard library template "std::is_specialization_of"
> inside libstdc++, and I did so as
> follows:
>
> template< typename T, template<typename...> class
> __any_template_template_parameter >
> constexpr bool is_specialization_of = __is_specialization_of(T,
> __any_template_template_parameter);
>
> In the compiler core code, I gave special treatment to any parameter
> named '__any_template_template_parameter'. Parameters with this name
> will accept any template template parameter, even "std::array" which
> has a constant instead of a type.
>
> Here is the patch:
>
>
> https://github.com/healytpk/gcc-thomas-healy/commit/tag_is_specialization_of
>
> And here it is tested and working:
>
> https://godbolt.org/z/oWsq1nKs5
>
> So I think perhaps other compilers should do the same thing -- use
> magic behind the scenes to make 'is_specialization_of' possible.
>
> And as for the possibility of one day having:
>
> template<typename_or_constant...> class Class;
>
> Well one complication would be that types and variables can have the
> same name in C++. Consider the following program:
>
> #include <stddef.h>
>
> int main(void)
> {
> constexpr char size_t = 0;
> alignas(size_t) char c;
> }
>
> In the second line, 'size_t' is treated as the variable (not the
> type). And so if you specify a number of types and constants to a
> template, will they be variables or types? Or will we need to write
> 'typename' before every type? Not a big issue but just one thing to
> think about.
>
> For now though we can have 'is_specialization_of' in the Standard with
> compiler support.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2026-01-17 22:11:13