Prüfen, ob eine Klasse eine Instanziierung eines Templates ist



  • Hi zusammen,

    ich möchte über template Metaprogramming und type_traits prüfen, ob eine Klasse eine Instanziierung eines bestimmten templates ist. Leider übersteigt mein Vorhaben meine Kenntnisse, kann mir hier jemand auf die Sprünge helfen?

    Hier mein bisher bester Versuch, basierend auf diesem Ansatz.

    #include <type_traits>
    
    template<std::int64_t num, std::int64_t den>
    struct Base
    {
        static constexpr std::int64_t Num = num;
        static constexpr std::int64_t Den = den;
    };
    
    template<std::int64_t num, std::int64_t den>
    struct Derived1 : Base<num,den>
    {
    };
    
    template<std::int64_t num, std::int64_t den>
    struct Derived2 : Base<num,den>
    {
    };
    
    template<typename T, typename U>
    static constexpr bool is_specialisation_of_v = std::false_type{};
    
    template<template<std::int64_t, std::int64_t> typename T,
             std::int64_t n, std::int64_t d,
             template<std::int64_t, std::int64_t> typename U>
    static constexpr bool is_specialisation_of_v<T<n,d>, U> = std::true_type{};
    
    int main()
    {
        using t1 = Derived1<1,1>;
        using t2 = Derived2<1,1>;
         
        bool const b1 = is_specialisation_of_v<t1,Derived1>;
    }
    

    Hier die dazugehörigen Fehlermeldungen:

    prog.cc:27:54: error: use of template template parameter 'U' requires template arguments
       27 | static constexpr bool is_specialisation_of_v<T<n,d>, U> = std::true_type{};
          |                                                      ^
    prog.cc:26:56: note: template is declared here
       26 |          template<std::int64_t, std::int64_t> typename U>
          |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~          ^
    prog.cc:34:47: error: use of class template 'Derived1' requires template arguments
       34 |     bool const b1 = is_specialisation_of_v<t1,Derived1>;
          |                                               ^
    prog.cc:12:8: note: template is declared here
       11 | template<std::int64_t num, std::int64_t den>
          | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       12 | struct Derived1 : Base<num,den>
          |        ^
    2 errors generated.