以下のプログラムも示すように、一般に、テンプレート引数は抽象クラスにすることができます。しかし、ソート中の比較ファンクターは抽象的であってはならないようです。少なくとも、以下はVC++11およびOracleStudio12ではコンパイルされません。
#include <vector>
#include <algorithm>
class Functor
{
public:
virtual bool operator()(int a, int b) const = 0;
};
class MyFunctor: public Functor
{
public:
virtual bool operator()(int a, int b) const { return true; }
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<Functor> fv; // template of abstract class is possible
vector<int> v;
MyFunctor* mf = new MyFunctor();
sort(v.begin(), v.end(), *mf);
Functor* f = new MyFunctor();
// following line does not compile:
// "Cannot have a parameter of the abstract class Functor"
sort(v.begin(), v.end(), *f);
return 0;
}
さて、これはファンクター引数の一般的なプロパティなのか、それともSTLの実装に依存するのか疑問に思います。私がやりたかったことを取得する方法はありますか?