c++ - 使用 std::enable_if 的多变量模板特化
问题描述
我试图用这些有效值简洁地定义一个变量模板:
// (template<typename T> constexpr T EXP = std::numeric_limits<T>::max_exponent / 2;)
// float and double scalar definitions:
const double huge = std::scalbn(1, EXP<double>);
const float huge = std::scalbn(1, EXP<float>);
// SIMD vector definitions:
const Vec8f huge = Vec8f(huge<float>); // vector of 8 floats
const Vec8d huge = Vec8d(huge<double>); // vector of 8 doubles
const Vec4f huge = Vec4f(huge<float>); // vector of 4 floats
// Integral types should fail to compile
VecXX 向量定义(SIMD 向量)需要使用如图所示的相应标量类型(例如huge<float>
,对于 s 的向量float
)。这可以作为VecXX::value_type
或通过类型特征样式模板类 ( VectorTraits<VecXX>::value_type
) 获得。
理想情况下,我想我会有类似的东西:
// Primary. What should go here? I want all other types to not compile
template<typename T, typename Enabler = void>
const T huge = T{ 0 };
// Scalar specialization for floating point types:
template<typename T>
const T huge<T> = std::enable_if_t<std::is_floating_point<T>::value, T>(std::scalbn(1, EXP<T>));
// Vector specialization, uses above declaration for corresponding FP type
template<typename T>
const T huge<T> = std::enable_if_t<VectorTraits<T>::is_vector, T>(huge<VectorTraits<T>::scalar_type>);
但我不能完全弄清楚一个工作版本(上面失败了“重新定义const T huge<T>
”)。最好的方法是什么?
解决方案
不完全是您所问的,但我希望以下示例可以向您展示如何使用 SFINAE 来专门化模板变量
template <typename T, typename = void>
constexpr T huge = T{0};
template <typename T>
constexpr T huge<T, std::enable_if_t<std::is_floating_point<T>{}>> = T{1};
template <typename T>
constexpr T huge<std::vector<T>> = T{2};
你可以检查它
std::cout << huge<int> << std::endl;
std::cout << huge<long> << std::endl;
std::cout << huge<float> << std::endl;
std::cout << huge<double> << std::endl;
std::cout << huge<long double> << std::endl;
std::cout << huge<std::vector<int>> << std::endl;
推荐阅读
- android - APK 内置 Ionic 与 React 和 Capacitor 与 Razorpay 但没有得到结帐页面,任何人请给一些想法来解决它?
- c++ - 使用 STAN 数学对 C++ 中的矩阵进行自动微分
- reactjs - 反应我希望空白在菜单显示之前消失
- z3 - Z3 定理证明器是否有语法规范?
- sql - 使用 SQL 进行登录、注销跟踪
- java - READ_EXTERNAL_STORAGE 是否可以根据情况在 Android 11 中提供不同程度的访问权限?
- r - 如何在 Tidymodel 食谱中进行 group_by
- typescript - 从存储库引用的打字稿包 - 编译失败并出现奇怪的错误
- unicode - Pydap open_url UnicodeDecodeError
- woocommerce - 在单个产品页面中以图像显示产品属性