Boost::multiprecision与cardinal_cubic_b_spline结合编译失败求助
问题分析与解决方案
问题原因
Boost cardinal_cubic_b_spline 的实现逻辑中,部分代码假设数值类型的运算会直接返回自身类型,但 mpf_float_50、mpfr_float_50 等高精度类型默认启用了**表达式模板(Expression Templates)**机制,运算后返回的是模板化的表达式对象而非原始数值类型,这与插值器预期的类型不兼容,最终导致编译时出现类型转换错误。而 cpp_bin_float_50 默认未启用表达式模板,因此可以正常编译。
解决方案
禁用目标高精度类型的表达式模板,强制运算直接返回原始数值类型。只需在定义高精度类型时指定 expression_template_option::et_off 参数即可实现。
修改后的示例代码
#include <boost/math/interpolators/cardinal_cubic_b_spline.hpp> #include <iostream> #include <boost/multiprecision/gmp.hpp> // 禁用表达式模板,强制返回原始数值类型 using mpf_float_50 = boost::multiprecision::number< boost::multiprecision::backends::gmp_float<50>, boost::multiprecision::expression_template_option::et_off >; int main() { std::vector<mpf_float_50> v(10); mpf_float_50 step(0.01); for (size_t i = 0; i < v.size(); ++i) { v.at(i) = sin(i*step); } mpf_float_50 leftPoint(0.0); boost::math::interpolators::cardinal_cubic_b_spline<mpf_float_50> spline(v.begin(), v.end(), leftPoint, step); mpf_float_50 x(3.1); mpf_float_50 tmpVal = spline(x); std::cout << tmpVal << std::endl; return 0; }
适配其他高精度类型
对于 mpfr_float_50、cpp_dec_float_50 等类型,只需用同样的方式禁用表达式模板即可:
// 以mpfr_float_50为例 using mpfr_float_50 = boost::multiprecision::number< boost::multiprecision::backends::mpfr_float<50>, boost::multiprecision::expression_template_option::et_off >;
内容的提问来源于stack exchange,提问作者orion
相关产品推荐
相关产品推荐

