C++ C++;模板专门化问题

C++ C++;模板专门化问题,c++,template-specialization,C++,Template Specialization,我的语法与此类似: template <class P> class Foo : public P { public: template <class T> Foo<P> & operator += (const T & v) { /*grind*/ return (*this); } }; 但是部分专业化是不可能的,在这一点上几乎不可能知道p的类型 有什么解决办法吗 谢谢, Ra

我的语法与此类似:

template <class P>
class Foo : public P
{
public:
    template <class T> Foo<P> & operator += (const T & v)
    { 
         /*grind*/
         return (*this);
    }
};
但是部分专业化是不可能的,在这一点上几乎不可能知道
p
的类型

有什么解决办法吗

谢谢,
Raxvan。

将逻辑放在
操作符+=
中,而不是放在助手类中,这可以根据需要进行部分专门化。

节点比我快,但这里有一些代码可能会有所帮助(尽管我使用带重载的裸函数而不是助手类):

模板
Foo类:公共P类
{
公众:
模板Foo

&运算符+=(常量T&v) { 细节::实现_加_等于(*此,v); 归还*这个; } }; 名称空间详细信息{ 模板 无效执行加等于(Foo

&f,int v) { /*研磨整型*/ } 模板 无效执行加等于(Foo

&f,T const&v) { /*磨碎*/ } }


为什么需要在类外执行此操作?如果您不需要在类定义外执行此操作,这将很简单。这是我最初的想法,但我最终编写了太多代码。无论如何,谢谢你,但我想我有一个解决办法。我刚刚意识到我可以移动P类中的部分功能,消除了专门化的需要。Jollymorphic打开了我的思路,我觉得太复杂了。帮手班可以工作:)哎呀,谢谢,我还有别的想法。这似乎也是一个很好的解决方案:)。
template <class P> Foo<P>& Foo<P>::operator += ( const int & v)
{...}
template <class P>
class Foo : public P
{
public:
   template <class T> Foo<P> & operator += (const T & v)
   { 
      detail::implement_plus_equal( * this, v );
      return * this;
   }
};

namespace detail {
   template <class P>
   void implement_plus_equal(Foo<P> & f, int v)
   {
      /* grind an int */
   }

   template <class P, typename T>
   void implement_plus_equal(Foo<P> & f, T const & v)
   {
      /* grind a T */
   }
}