<small id='BWoLK'></small><noframes id='BWoLK'>

    • <bdo id='BWoLK'></bdo><ul id='BWoLK'></ul>

        <i id='BWoLK'><tr id='BWoLK'><dt id='BWoLK'><q id='BWoLK'><span id='BWoLK'><b id='BWoLK'><form id='BWoLK'><ins id='BWoLK'></ins><ul id='BWoLK'></ul><sub id='BWoLK'></sub></form><legend id='BWoLK'></legend><bdo id='BWoLK'><pre id='BWoLK'><center id='BWoLK'></center></pre></bdo></b><th id='BWoLK'></th></span></q></dt></tr></i><div id='BWoLK'><tfoot id='BWoLK'></tfoot><dl id='BWoLK'><fieldset id='BWoLK'></fieldset></dl></div>
        <legend id='BWoLK'><style id='BWoLK'><dir id='BWoLK'><q id='BWoLK'></q></dir></style></legend>
      1. <tfoot id='BWoLK'></tfoot>
      2. const 和非 const 模板特化

        时间:2023-05-24
      3. <tfoot id='fmVni'></tfoot>
        <legend id='fmVni'><style id='fmVni'><dir id='fmVni'><q id='fmVni'></q></dir></style></legend>

          <tbody id='fmVni'></tbody>
                <bdo id='fmVni'></bdo><ul id='fmVni'></ul>
                <i id='fmVni'><tr id='fmVni'><dt id='fmVni'><q id='fmVni'><span id='fmVni'><b id='fmVni'><form id='fmVni'><ins id='fmVni'></ins><ul id='fmVni'></ul><sub id='fmVni'></sub></form><legend id='fmVni'></legend><bdo id='fmVni'><pre id='fmVni'><center id='fmVni'></center></pre></bdo></b><th id='fmVni'></th></span></q></dt></tr></i><div id='fmVni'><tfoot id='fmVni'></tfoot><dl id='fmVni'><fieldset id='fmVni'></fieldset></dl></div>
                1. <small id='fmVni'></small><noframes id='fmVni'>

                2. 本文介绍了const 和非 const 模板特化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个类模板,用于获取变量的大小:

                  I have a class template which I use to get the size of a variable:

                  template <class T>
                  class Size
                  {
                     unsigned int operator() (T) {return sizeof(T);}
                  };
                  

                  这很好用,但对于字符串我想使用 strlen 而不是 sizeof:

                  This works fine but for strings I want to use strlen instead of sizeof:

                  template <>
                  class Size<char *>
                  {
                     unsigned int operator() (char *str) {return strlen(str);}
                  };
                  

                  问题是当我用 const char * 创建一个 size 实例时,它会转到非专业化版本.我想知道是否有办法在模板特化中同时捕获 char * 的常量和非常量版本?谢谢.

                  The problem is when I create an instance of size with const char * it goes to the unspecialized version. I was wondering if there is a way to capture both the const and non-const versions of char * in on template specialization? Thanks.

                  推荐答案

                  使用这个技巧:

                  #include <type_traits>
                  
                  template< typename T, typename = void >
                  class Size
                  {
                    unsigned int operator() (T) {return sizeof(T);}
                  };
                  
                  template< typename T >
                  class Size< T, typename std::enable_if<
                                   std::is_same< T, char* >::value ||
                                   std::is_same< T, const char* >::value
                                 >::type >
                  {
                    unsigned int operator() ( T str ) { /* your code here */ }
                  };
                  

                  如何在类定义之外定义方法的示例.

                  Example of how to define the methods outside of the class definition.

                  添加了帮助程序以避免重复可能的冗长和复杂的条件.

                  Added helper to avoid repeating the possibly long and complex condition.

                  简化的助手.

                  #include <type_traits>
                  #include <iostream>
                  
                  template< typename T >
                  struct my_condition
                    : std::enable_if< std::is_same< T, char* >::value ||
                                      std::is_same< T, const char* >::value >
                  {};
                  
                  template< typename T, typename = void >
                  struct Size
                  {
                    unsigned int operator() (T);
                  };
                  
                  template< typename T >
                  struct Size< T, typename my_condition< T >::type >
                  {
                    unsigned int operator() (T);
                  };
                  
                  template< typename T, typename Dummy >
                  unsigned int Size< T, Dummy >::operator() (T)
                  {
                    return 1;
                  }
                  
                  template< typename T >
                  unsigned int Size< T, typename my_condition< T >::type >::operator() (T)
                  {
                    return 2;
                  }
                  
                  int main()
                  {
                    std::cout << Size< int >()(0) << std::endl;
                    std::cout << Size< char* >()(0) << std::endl;
                    std::cout << Size< const char* >()(0) << std::endl;
                  }
                  

                  哪个打印

                  1
                  2
                  2
                  

                  这篇关于const 和非 const 模板特化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:正在铸造 std::pair&lt;T1, T2&gt;常量&amp;到 下一篇:计算以字节为单位设置的位数

                  相关文章

                  最新文章

                    <small id='K73qM'></small><noframes id='K73qM'>

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

                    1. <legend id='K73qM'><style id='K73qM'><dir id='K73qM'><q id='K73qM'></q></dir></style></legend>

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