<tfoot id='q79CF'></tfoot>

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

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

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

        std::enable_if :参数与模板参数

        时间:2023-05-23
          <i id='07QoK'><tr id='07QoK'><dt id='07QoK'><q id='07QoK'><span id='07QoK'><b id='07QoK'><form id='07QoK'><ins id='07QoK'></ins><ul id='07QoK'></ul><sub id='07QoK'></sub></form><legend id='07QoK'></legend><bdo id='07QoK'><pre id='07QoK'><center id='07QoK'></center></pre></bdo></b><th id='07QoK'></th></span></q></dt></tr></i><div id='07QoK'><tfoot id='07QoK'></tfoot><dl id='07QoK'><fieldset id='07QoK'></fieldset></dl></div>

              <tbody id='07QoK'></tbody>
            • <tfoot id='07QoK'></tfoot>
            • <legend id='07QoK'><style id='07QoK'><dir id='07QoK'><q id='07QoK'></q></dir></style></legend>

                  <bdo id='07QoK'></bdo><ul id='07QoK'></ul>

                  <small id='07QoK'></small><noframes id='07QoK'>

                • 本文介绍了std::enable_if :参数与模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在构建一些输入检查器,它需要具有整数和/或双精度的特定函数(例如,isPrime"应该仅适用于整数).

                  I'm building some input checker that needs to have specific functions for integer and/or double (for example 'isPrime' should only be available for integers).

                  如果我使用 enable_if 作为参数,它运行良好:

                  If I'm using enable_if as a parameter it's working perfectly :

                  template <class T>
                  class check
                  {
                  public:
                     template< class U = T>
                     inline static U readVal(typename std::enable_if<std::is_same<U, int>::value >::type* = 0)
                     {
                        return BuffCheck.getInt();
                     }
                  
                     template< class U = T>
                     inline static U readVal(typename std::enable_if<std::is_same<U, double>::value >::type* = 0)
                     {
                        return BuffCheck.getDouble();
                     }   
                  };
                  

                  但是如果我将它用作模板参数(如 http://en.cppreference.com/w/cpp/types/enable_if )

                  but if I'm using it as a template paramater (as demonstrated on http://en.cppreference.com/w/cpp/types/enable_if )

                  template <class T>
                  class check
                  {
                  public:
                     template< class U = T, class = typename std::enable_if<std::is_same<U, int>::value>::type >
                     inline static U readVal()
                     {
                        return BuffCheck.getInt();
                     }
                  
                     template< class U = T, class = typename std::enable_if<std::is_same<U, double>::value>::type >
                     inline static U readVal()
                     {
                        return BuffCheck.getDouble();
                     }
                  };
                  

                  然后我有以下错误:

                  error: ‘template<class T> template<class U, class> static U check::readVal()’ cannot be overloaded
                  error: with ‘template<class T> template<class U, class> static U check::readVal()’
                  

                  我不知道第二个版本有什么问题.

                  I can't figure out what is wrong in the second version.

                  推荐答案

                  默认模板参数不是模板签名的一部分(因此两个定义都尝试定义相同的模板两次).然而,它们的参数类型是签名的一部分.所以你可以这样做

                  Default template arguments are not part of the signature of a template (so both definitions try to define the same template twice). Their parameter types are part of the signature, however. So you can do

                  template <class T>
                  class check
                  {
                  public:
                     template< class U = T, 
                               typename std::enable_if<std::is_same<U, int>::value, int>::type = 0>
                     inline static U readVal()
                     {
                        return BuffCheck.getInt();
                     }
                  
                     template< class U = T, 
                               typename std::enable_if<std::is_same<U, double>::value, int>::type = 0>
                     inline static U readVal()
                     {
                        return BuffCheck.getDouble();
                     }
                  };
                  

                  这篇关于std::enable_if :参数与模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何 typedef 模板类? 下一篇:如何在 C++ 中将 typename T 转换为字符串

                  相关文章

                  最新文章

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

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