<legend id='mTC1K'><style id='mTC1K'><dir id='mTC1K'><q id='mTC1K'></q></dir></style></legend>
    • <bdo id='mTC1K'></bdo><ul id='mTC1K'></ul>
    1. <small id='mTC1K'></small><noframes id='mTC1K'>

      <i id='mTC1K'><tr id='mTC1K'><dt id='mTC1K'><q id='mTC1K'><span id='mTC1K'><b id='mTC1K'><form id='mTC1K'><ins id='mTC1K'></ins><ul id='mTC1K'></ul><sub id='mTC1K'></sub></form><legend id='mTC1K'></legend><bdo id='mTC1K'><pre id='mTC1K'><center id='mTC1K'></center></pre></bdo></b><th id='mTC1K'></th></span></q></dt></tr></i><div id='mTC1K'><tfoot id='mTC1K'></tfoot><dl id='mTC1K'><fieldset id='mTC1K'></fieldset></dl></div>

      <tfoot id='mTC1K'></tfoot>

      1. 如何测试类 B 是否派生自类的模板族

        时间:2023-05-24

          <tfoot id='rSlrD'></tfoot>
            • <bdo id='rSlrD'></bdo><ul id='rSlrD'></ul>

                <tbody id='rSlrD'></tbody>
              <legend id='rSlrD'><style id='rSlrD'><dir id='rSlrD'><q id='rSlrD'></q></dir></style></legend>
              <i id='rSlrD'><tr id='rSlrD'><dt id='rSlrD'><q id='rSlrD'><span id='rSlrD'><b id='rSlrD'><form id='rSlrD'><ins id='rSlrD'></ins><ul id='rSlrD'></ul><sub id='rSlrD'></sub></form><legend id='rSlrD'></legend><bdo id='rSlrD'><pre id='rSlrD'><center id='rSlrD'></center></pre></bdo></b><th id='rSlrD'></th></span></q></dt></tr></i><div id='rSlrD'><tfoot id='rSlrD'></tfoot><dl id='rSlrD'><fieldset id='rSlrD'></fieldset></dl></div>

              • <small id='rSlrD'></small><noframes id='rSlrD'>

                  本文介绍了如何测试类 B 是否派生自类的模板族的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  如何在编译时测试B类是否是从std::vector派生的?

                  How to test at compile time whether class B is derived from std::vector?

                  template<class A>
                  struct is_derived_from_vector {
                    static const bool value = ????;
                  };
                  

                  如何在编译时测试B类是否派生自模板族?

                  How to test at compile time whether class B is derived from template family?

                  template<class A, template< class > class Family>
                  struct is_derived_from_template {
                    static const bool value = ????;
                  };
                  

                  使用:

                  template<class T> struct X {};
                  
                  struct A : X<int> {}
                  struct B : std::vector<char> {}
                  struct D : X<D> {}
                  
                  int main() {
                     std::cout << is_derived_from_template<A, X>::value << std::endl; // true
                     std::cout << is_derived_from_template<D, X>::value << std::endl; // true
                     std::cout << is_derived_from_vector<A>::value << std::endl; // false
                     std::cout << is_derived_from_vector<B>::value << std::endl; // true
                  }
                  

                  推荐答案

                  试试这个:

                  #include <type_traits>
                  
                  template <typename T, template <typename> class Tmpl>  // #1 see note
                  struct is_derived
                  {
                      typedef char yes[1];
                      typedef char no[2];
                  
                      static no & test(...);
                  
                      template <typename U>
                      static yes & test(Tmpl<U> const &);
                  
                      static bool const value = sizeof(test(std::declval<T>())) == sizeof(yes);
                  };
                  

                  用法:

                  #include <iostream>
                  
                  template<class T> struct X {};
                  
                  struct A : X<int> {};
                  
                  int main()
                  {
                      std::cout << is_derived<A, X>::value << std::endl;
                      std::cout << is_derived<int, X>::value << std::endl;
                  }
                  

                  注意:在标记为 #1 的行中,您还可以让您的 trait 接受任何 模板,该模板至少有一个,但可能writint 的更多类型参数:

                  Note: In the line marked #1, you could also make your trait accept any template that has at least one, but possibly more type arguments by writint:

                  template <typename, typename...> class Tmpl
                  

                  这篇关于如何测试类 B 是否派生自类的模板族的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:删除“通用"的专用模板函数案例无法使用 下一篇:为什么“A&lt;0&gt;=0"中的模板id由于大于

                  相关文章

                  最新文章

                • <small id='y6R1a'></small><noframes id='y6R1a'>

                  <tfoot id='y6R1a'></tfoot>
                • <i id='y6R1a'><tr id='y6R1a'><dt id='y6R1a'><q id='y6R1a'><span id='y6R1a'><b id='y6R1a'><form id='y6R1a'><ins id='y6R1a'></ins><ul id='y6R1a'></ul><sub id='y6R1a'></sub></form><legend id='y6R1a'></legend><bdo id='y6R1a'><pre id='y6R1a'><center id='y6R1a'></center></pre></bdo></b><th id='y6R1a'></th></span></q></dt></tr></i><div id='y6R1a'><tfoot id='y6R1a'></tfoot><dl id='y6R1a'><fieldset id='y6R1a'></fieldset></dl></div>

                      <legend id='y6R1a'><style id='y6R1a'><dir id='y6R1a'><q id='y6R1a'></q></dir></style></legend>

                        <bdo id='y6R1a'></bdo><ul id='y6R1a'></ul>