21

注意:这似乎是一个问题的转贴:C++ - Overload templated class method with a partial specilization

我将 C++ 模板专业化遇到的问题归结为一个简单的案例。

它由一个简单的 2 参数模板类组成Thing,我想专门Thing<A,B>::doSomething()针对B=int.

#include <cstdio>

// A 3-parameter template class.
template <class A, class B>
class Thing
{
public:
    Thing(A a, B b) : a_(a), b_(b) {}
    B doSomething();
private:
    A a_;
    B b_;
};

// The generic case works as expected.
template <class A, class B>
B Thing<A,B>::doSomething()
{
    return b_;
}

// This specialization does not work!
template <class A>
int Thing<A,int>::doSomething()
{
    return b_+1;
}

int main() {
    // Setup our thing.
    Thing<double,int> thing(1.0,2);
    // This doesn't compile - but works with the generic case.
    printf("Expecting 3, and getting %i\n", thing.doSomething());
    // Clean up.
    return 0;
}

不幸的是,g++退出并出现错误:

partial_specialization.cpp:30: error: invalid use of incomplete type ‘class Thing<A, int>’
partial_specialization.cpp:8: error: declaration of ‘class Thing<A, int>’

编译器clang++有点冗长,但有同样的问题:

partial_specialization.cpp:30:19: error: nested name specifier 'Thing<A, int>::' for declaration does not
      refer into a class, class template or class template partial specialization
int Thing<A,int>::doSomething()
    ~~~~~~~~~~~~~~^
partial_specialization.cpp:32:12: error: use of undeclared identifier 'b_'
    return b_+1;
           ^
2 errors generated.

我已阅读并理解不允许对函数进行部分模板专业化 - 但我认为Thing在这种情况下我部分专业化了类。

有任何想法吗?

我做了什么:一种解决方法,由接受的答案提供的链接确定:

template< class T >
inline T foo( T const & v ) { return v; }

template<>
inline int foo( int const & v ) { return v+1; }

// The generic case works as expected.
template <class A, class B>
B Thing<A,B>::doSomething()
{
    return foo(b_);
}
4

2 回答 2

46

标准不允许函数模板的部分特化,无论是成员函数模板还是独立函数模板:

template<typename T, typename U> void f() {} //okay  - primary template
template<typename T> void f<T,int>() {}      //error - partial specialization
template<> void f<unsigned char,int>() {}    //okay  - full specialization

但是您可以部分专门化类模板本身。你可以这样做:

template <class A>
class Thing<A,int>  //partial specialization of the class template
{
    //..
    int doSomething();
};

template <class A>
int Thing<A,int>::doSomething()  { /* do whatever you want to do here */ }

请注意,当您部分特化类模板时,成员函数的模板参数列表(在其类外的定义中)必须与类模板部分特化的模板参数列表匹配。这意味着,对于类模板的上述部分特化,您不能定义它:

template <class A>
int Thing<A,double>::doSomething(); //error

这是不允许的,因为函数定义中的模板参数列表与类模板部分特化的模板参数列表不匹配。标准(2003)中的§14.5.4.3/1 说,

类模板部分特化成员的模板参数列表应与类模板部分特化的模板参数列表匹配。[...]

有关更多信息,请在此处阅读我的答案:

C++ - 使用该方法的部分特化重载模板化类方法


那么解决方案是什么?您会在所有重复性工作中部分专业化您的课程吗?

一个简单的解决方案是工作委托,而不是部分专门化类模板。编写一个独立的函数模板并将其专门化为:

template <class B>
B doTheActualSomething(B & b) { return b;  }

template <>
int doTheActualSomething<int>(int & b) { return b + 1; }

doSomething()然后从成员函数调用这个函数模板:

template <class A, class B>
B Thing<A,B>::doSomething() { return doTheActualSomething<B>(b_); }

由于在您的特定情况下,只doTheActualSomething需要知道一个成员的值,即b_,上述解决方案很好,因为您可以将值作为参数传递给函数,其类型为模板类型参数B,并且int可以专门化全专业化。

但是想象一下,如果它需要访问多个成员,每个成员的类型取决于模板类型参数列表,那么定义一个独立的函数模板并不能解决问题,因为现在函数的类型参数将不止一个模板,并且您不能仅将功能部分专门化为一种类型(因为它是不允许的)。

所以在这种情况下,您可以定义一个类模板,它定义了一个静态非模板成员函数doTheActualSomething。方法如下:

template<typename A, typename B>
struct Worker
{
   B doTheActualSomething(Thing<A,B> *thing)
   {
      return thing->b_;
   }
};

//partial specialization of the class template itself, for B = int
template<typename A>
struct Worker<A,int>
{
   int doTheActualSomething(Thing<A,int> *thing)
   {
      return thing->b_ + 1;
   }
};

请注意,您可以使用thing指针来访问该类的任何成员。当然,如果它需要访问私有成员,那么你就得结交类模板struct Worker的朋友,如:Thing

//forward class template declaration
template<typename T, typename U> struct Worker

template <class A, class B>
class Thing
{
    template<typename T, typename U>  friend struct Worker; //make it friend
   //...
};

现在将工作委托给朋友:

template <class A, class B>
B Thing<A,B>::doSomething()
{
    return Worker<A,B>::doTheActualSomething(this); //delegate work
}

这里需要注意两点:

  • 在这个解决方案中,doTheActualSomething不是成员函数模板。它不包含模板类。因此,我们可以随时对类模板进行部分特化,以获得部分成员函数模板特化的预期效果。
  • 由于我们将this指针作为参数传递给函数,我们可以访问类的任何成员Thing<A,B>,甚至是私有成员,Worker<T,U>也是朋友。

完整的在线演示:http ://www.ideone.com/uEQ4S


现在还有改进的机会。现在Worker类模板的所有实例化都是类模板的所有实例化的朋友Thing。所以我们可以将这种多对多的友谊限制为:

template <class A, class B>
class Thing
{
    friend struct Worker<A,B>; //make it friend
   //...
};

现在只有一个类模板实例化Worker是一个类模板实例化的朋友Thing。那是一对一的友谊。也就是说,Worker<A,B>是 的朋友Thing<A,B>Worker<A,B>不是 的朋友Thing<A,C>

这种变化要求我们以稍微不同的顺序编写代码。请参阅完整的演示,其中包含类和函数定义的所有顺序以及所有:

http://www.ideone.com/6a1Ih

于 2011-05-26T12:29:55.737 回答
9

这是一个非常常见的问题,并且有一个非常简单的解决方案。我将在一个人工示例中展示它,因为它比使用您的代码更清晰,并且您必须理解它才能使其适应您的代码

template<typename A, typename B>
struct TwoTypes { };

template<typename A, typename B>
struct X {
  /* forwards ... */
  void f() { fImpl(TwoTypes<A, B>()); }

  /* special overload for <A, int> */
  template<typename A1>
  void fImpl(TwoTypes<A1, int>) {
    /* ... */
  }

  /* generic */
  template<typename A1, typename B1>
  void fImpl(TwoTypes<A1, B1>) {
    /* ... */
  }
};

明确地专门化函数从来都不是(几乎从来没有?)正确的方法。在我作为程序员的工作中,我从未明确专门化过函数模板。重载和偏序是优越的。

于 2011-05-26T17:42:19.087 回答