私は、整数型(short、int、long)と浮動小数点型(float、double)の両方をとるMatrixクラスに取り組んでいます。一部のメソッドを浮動小数点型(反転メソッドなど)のみに制限し、一部のメソッドに浮動小数点型と整数型(==演算子など)の実装を変えたいと思います。Boostの「enable_if」と「is_integral」/「is_floating_point」を使用するのが正しい方法だと思いますが、それを機能させることができないようです。
私の実装は、このc++セミ擬似コードに似ています。
template <typename T>
class Matrix
{
...
bool operator==(Matrix<typename enable_if<is_integral<T> T >::type >) const;
bool operator==(Matrix<typename enable_if<is_floating_point<T>::type T> >) const;
typename enable_if<is_floating_point<T> T> computeInverse() const;
...
};
// implementation
bool Matrix<T>::operator==(Matrix<typename enable_if<is_integral<T> T >::type >) const {
//implementation without precision
}
bool Matrix<T>::operator==(Matrix<typename enable_if<is_integral<T> T >::type >) const {
//implementation using precision
}
Matrix<typename enable_if<is_floating_point<T> T>::type > Matrix<T>::computeInverse() const {
//implementation requiring floating points
}
これは多くのコンパイルエラーを生成しますが、私はこれらが最も関連性のあるものだと思います:
error: no type named ‘type’ in ‘struct boost::enable_if<boost::is_integral<float>, float>’
と
error: no type named ‘type’ in ‘struct boost::enable_if<boost::is_floating_point<int>, int>’
これは、少なくともboostのenable_ifを使用しない限り、タイプごとに異なる実装を行うことができないことを示しています。これは正しいですか?
もしそうなら、どうすればこれを行うことができますか?テンプレートの特殊化が進むべき道であることは知っていますが、コードの重複が多すぎないようにしたいと思います。