C++ Logo


Advanced search

[std-proposals] Compiler support for is_specialization_of

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 26 Mar 2024 12:39:44 +0000
Looking at P2098r1:


The given implementation works fine for specialisations of 'vector', but
not for specialisations of 'array', as follows:

#include <cstddef>
#include <array>
#include <type_traits>
#include <vector>

namespace std {
  template< class T , template<class...> class Primary >
  struct is_specialization_of : false_type {};
  template< template<class...> class Primary , class... Args >
  struct is_specialization_of< Primary<Args...>, Primary> : true_type {};

int main(void)
    using std::is_specialization_of;
    static_assert( is_specialization_of< std::vector<int>, std::vector
>::value );
    static_assert( is_specialization_of< std::array<int, 5>, std::array
>::value );

Compiler output:

a.cpp: In function 'int main()':
a.cpp:22:73: error: type/value mismatch at argument 2 in template parameter
list for 'template<class T, template<class ...> class Primary> struct
a.cpp:22:73: note: expected a template of type 'template<class ...> class
Primary', got 'template<class _Tp, long unsigned int _Nm> struct std::array'

If 'is_specialization_of' is to be written into the Standard, shall it be
worded that it requires compiler support to let it work with every template
class (and not just the ones whose parameters are exclusively all types)?
Or will the use of this feature be restricted to template classes whose
parameters are all types?

I'm talking something like:

namespace std {
  template< class T , /* COMPILER_SUPPORT */ Primary >
  struct is_specialization_of : false_type {};
  template< /* COMPILER_SUPPORT */ Primary , class... Args >
  struct is_specialization_of< Primary<Args...>, Primary> : true_type {};

Received on 2024-03-26 12:39:47