一部の Timestamp プロパティ (クラスは から継承) をチェックするテンプレート関数を作成したいと考えてTimed
いますが、タイムスタンプを持たない型に対しても機能する必要があります。私が見つけた最良の(そしてまだかなり醜い)解決策は次のとおりです。
class Timed {
protected:
int mTime;
public:
explicit Timed(int time=0): mTime(time){}
int getT() const {return mTime;}
};
template<typename T>
bool checkStale(T const* ptr) const {
return checkStaleImp(ptr, boost::is_base_of<Timed, T>() );
}
template<typename T>
template<bool b>
bool checkStaleImp(T const* ptr, boost::integral_constant<bool, b> const &){
return true;
}
template<typename T>
bool checkStaleImp(T const* ptr, boost::true_type const&){
const int oldest = 42;
return (42 <= ptr->getT());
}
これは、1 つの機能に対して 3 つの機能です。これを達成するためのより簡単な方法はありますboost::is_base_of
か? if 条件または boost::enable if で同様に、関数出力を から派生していないクラスの一種の定数に変換しますTimed
。残念ながら、仮想機能を使用したソリューションはオプションではありません。