私はC++で単純な数式を作成しようとしています。私はコンパイル時に計算するεを持つカスタム浮動小数点型を使いたい。ですから、タイプ控除操作を作成する必要があります。 そしてそこには、私は、このコードに問題がある:C++の宣言型式の控除
namespace Math {
namespace Internal {
template<typename typeT>
struct type_value {
static typeT value_;
};
template<typename type1T, typename type2T, size_t length>
struct dot_product_type_deducator {
using value_type =
decltype(
(type_value<dot_product_type_deducator<type1T, type2T, length - 1>>::value_)
+ (type_value<dot_product_type_deducator<type1T, type2T, 1>>::value_));
};
template<typename type1T, typename type2T>
struct dot_product_type_deducator<type1T, type2T, 1> {
using value_type = decltype(type_value<type1T>::value_ * type_value<type2T>::value_);
};
}
template<typename type1T, typename type2T, size_t length>
static auto dot(const Vector<type1T, length> &vector1,
const Vector<type2T, length> &vector2) {
return std::inner_product(vector1.begin(), vector1.end(), vector2.begin(),
static_cast<typename Internal::dot_product_type_deducator<type1T, type2T, length>::value_type>(0));
}
}
私が書くとき:
dot(Vector<int, 3>{1, 2, 3}, Vector<float, 3>{2, 3, 4})
MSVCは言う:
error C2676: binary '+': Math::Internal::dot_product_type_deducator<type1T,type2T,1>' does not define this operator or a conversion to a type acceptable to the predefined operator
1> with
1> [
1> type1T=int,
1> type2T=float
1> ]
そして、さらに2つの同じエラー。
回答:
私はちょうどdot_product_type_deducator::value_type
を書くのを忘れていました。
これを 'type1T、type2T、length-1> :: value_type> :: value_'に変更してください。 – 0x499602D2
本当にコードをスクラップして' inner_product(v1.begin()、v1。 – 0x499602D2
2つ以上の浮動小数点を合計すると、カスタムのεが不正確になることはありません。例えば、私は5の 'custom_float'のベクトルを持っています。私の操作の後に、それらを合計した後に(例えば、本当のthreesholdはもっと大きくなるはずです) 'custom_float <5 * epsilon>'となります。 –