【问题标题】:c++ multiple parameters templated class member specializationc++多参数模板化类成员特化
【发布时间】:2016-04-08 15:23:07
【问题描述】:

我正在设置一个 c++(11) 程序,其中我使用了一个依赖于 2 个参数的模板类。大部分类都可以根据模板参数进行通用编写。只有少数功能需要专门的版本。这是一个重现我的问题的示例模式:

template<class T, int N> 
class foo
{
  // typedefs and members that depend on T and N
  // but that can be written generically e.g. :
  typedef std::array<T,N> myarray;
  void myfunc(myarray tab);
};

// ...

template<class T, int N>
foo<T,N>::myfunc(myarray tab)
{
  // generic version
}

// need specialization only of myfunc:
template<class T>
foo<T,1>::myfunc(myarray tab)
{
  // specialized version for N=1
}

然后编译器抱怨:error: invalid use of incomplete type ‘class foo&lt;T, 1&gt;’ 关于然后行 template&lt;class T&gt; foo&lt;T,1&gt;::myfunc(myarray tab)

我发现唯一可行的解​​决方法是插入该类的完整副本及其专用版本:

template<class T>
class foo<T,1>
{
  // recopy all the lines of class foo<T,N>, replacing N by 1
};
// duplicate as well all generic function definition with 
// specialized versions <T,1> even when not needed

有什么很不满意的……

经过一些实验,我发现当模板仅使用1个参数时(例如template &lt;int N&gt; class foo{...};)似乎不会出现此问题,而是至少涉及2个参数时才会出现此问题。

这在 C++ 编程中是众所周知的吗?有没有更聪明的方法来解决我的问题? (我想创建一个没有专门功能的母类,然后让类 foo 从它继承,只保留专门的成员,以尽量减少“重复解决方法”)

感谢您的建议!

【问题讨论】:

    标签: c++ templates template-specialization


    【解决方案1】:

    标记调度以进行救援。

    使用std::integral_constant,我们可以创建两种类型,一种用于通用N,另一种用于1(或者您可以在int 上定义一些其他类型模板,但我选择使用已经存在)。

    void myfunc(myarray tab)
    {
        myfunchelper(tab, 
           typename std::is_same<std::integral_constant<int, N>,
                                 std::integral_constant<int, 1>>::type{});
    }
    

    它将调用调度到为std::true_typeN==1 的场景)和std::false_typeN != 1 的场景)重载的辅助函数:

    void myfunchelper(myarray tab, std::false_type);
    void myfunchelper(myarray tab, std::true_type);
    

    请注意,我故意不命名该类型,因为它没有被使用,并且智能编译器会优化该类型的任何类型的分配(我认为)。

    Live Demo

    (下面的演示代码):

    #include <array>
    #include <iostream>
    #include <type_traits>
    
    template<class T, int N> 
    class foo
    {
    public:
      // typedefs and members that depend on T and N
      // but that can be written generically e.g. :
      typedef std::array<T,N> myarray;
      void myfunc(myarray tab)
      {
          myfunchelper(tab, typename std::is_same<std::integral_constant<int, N>, std::integral_constant<int, 1>>::type{});
      }
      
    private:
      void myfunchelper(myarray tab, std::false_type)
      {
          std::cout << "Generic myfunc\n";
      }
      void myfunchelper(myarray tab, std::true_type)
      {
          std::cout << "myfunc specialized for N==1\n";
      }
    };
    
    int main()
    {
        std::array<char, 1> arr1{{'c'}};
        std::array<double, 2> arr2{{1.0, 2.0}};
        foo<char, 1> f1;
        foo<double, 2> f2;
        f1.myfunc(arr1); // calls specialized version
        f2.myfunc(arr2); // calls generic version
    }
    

    关于您发布的解决方案,这也是一种标签调度方法,它涉及定义另一个 int-template 类,该类将不必要的类型引入周围范围,还涉及强制转换 a 和 NULL (好吧,在这种情况下为 0,这就是 NULL 通常的 typedef'd as)。

    实际上,我发布的解决方案是相同的,但我认为它更清晰一点,并且更安全一点。

    【讨论】:

    • 感谢您的回复。确实,它比我建议的更干净、更直接。
    • 每增加一个新的专精,就会变得越来越笨拙。
    • @SergeyA。同意,但是对于像这样的小情况,当您需要对整个类进行部分专业化时,标签调度肯定会超过您所遭受的复制粘贴量。
    • @AndyG,也同意 - 这就是为什么我更喜欢父类方法:)
    【解决方案2】:

    简短的回答:您可以通过这种方式完全专业化成员,但不能部分专业化他们。

    长答案: 当你的类只有一个模板参数时,例如:

    template <class T> struct X {
       void foo() { };
    }
    
    template <> void X<int>::foo() {  }
    

    是完全专业化,并且是允许的。然而,

    template <class T, class Y> struct X {
       void foo() { };
    }
    
    template <class T> void X<int, Y>::foo() {  }
    

    是偏特化,单个成员的偏特化是不允许的——你需要对整个类进行部分特化。

    【讨论】:

    • 感谢您指出 C++ 规则。现在,有没有推荐的实现来避免不必要的代码重复?
    • @MathiasG,你自己已经提到过了。创建一个具有相同功能的基类,拥有一个公开派生自它的类,其方法应专门化,并为您的特殊类型对派生的整个类进行部分专门化。
    【解决方案3】:

    谷歌搜索Specialization of templated member function in templated class 并调整 Matthieu M. 的答案,我想我找到了解决问题的方法(重载):

    template<int N> class virtualclass{};
    
    template<class T, int N> 
    class foo
    {
      // typedefs and members that depend on T and N
      // but that can be written generically e.g. :
      typedef std::array<T,N> myarray;
      void myfunc(myarray tab)
      {
         helperfunc((virtualclass<N>*)0, tab);
      }
    
      template<class P>
      void helperfunc(P*, myarray tab);
    
      void helperfunc(virtualclass<1>*, myarray tab);
    };
    
    // ...
    
    template<class T, int N>
    template<class P>
    foo<T,N>::helperfunc(P*,myarray tab)
    {
      // generic version
    }
    
    // need specialization only of myfunc:
    template<class T, int N>
    foo<T,N>::helperfunc(virtualclass<1>*, myarray tab)
    {
      // specialized version for N=1
    }
    

    这看起来像是好的做法吗?这种解决方案可能有一些隐藏的缺点吗​​?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 2012-04-03
      相关资源
      最近更新 更多