c++ - 如何根据精度轻松模板化数学函数?
问题描述
假设我有两个已经预定义的数学函数:
float sqrtf(float a);
double sqrt(double a);
我有一些我用 C++ 编写的模板化代码,我想在两者之间自动切换:
template<typename Precision>
void foo(Precision arg) {
sqrt<Precision>(arg); // Call into sqrtf or sqrt depending on type?
}
有没有一种简单的方法可以完成上述操作,而无需手动重写所有重载?
为清楚起见进行编辑:这些是 C 和 CUDA 函数(例如 BLAS),它们没有预先构建的重载。
解决方案
多亏了 C++17 if constexpr
,它很简单
template <typename...>
struct always_false { static constexpr bool value = false; }; // used to make the static_asset value a dependent value otherwise it always fires
template<typename Precision>
void foo(Precision arg) {
if constexpr (std::is_same_v<Precision, double>)
sqrt(arg);
else if constexpr (std::is_same_v<Precision, float>)
sqrtf(arg);
else
static_assert(always_false<Precision>::value, "foo can only be called with the types double or float");
}
如果您不能使用 C++17,那么您可以专门化或重载foo
,double
并float
让这些专门化/重载调用正确的函数。
推荐阅读
- android - 尝试在 Kotlin 中使用相机拍照时如何解决 java.lang.SecurityException?
- leaflet - 如何将预定义的地点/标记添加到 Leaflet Geocoder
- assembly - mov fileName - 错误:操作码和操作数的组合无效
- python - 如何以比输入更小的准确度查询MongoDB中的一对坐标?
- ios - React Native 图像不会显示在一个应用程序上,但会显示在其他新应用程序上
- javascript - Javascript:如何在 jss 中使用相邻的兄弟选择器// 点击时改变颜色
- flutter - 在 Flutter 中更改页脚颜色。(持久页脚按钮)
- python - 如何以编程方式设置 SpanSelector 小部件的最小值和最大值
- c - C - Linux 在内核模式下创建声音
- angular - Angular 11,''router-outlet' 不是已知元素:'