c++ - 使用 C++20 概念的 static_assert 编译时参数检查
问题描述
我正在编写一个评估二项式系数的函数:binom(N, K)
我想在编译时检查 N 和 K 是否都是 N >= K 的无符号整数,但是有这个问题。
这是我的代码:
template <typename N>
concept Unsigned = std::is_unsigned_v<N>;
template <Unsigned U>
constexpr double binom(U N, U K) {
static_assert(N >= K);
double d = 1.0;
while (K) {
d *= static_cast<double>(N) / static_cast<double>(K);
K--;
N--;
}
return d;
}
template <Unsigned U>
constexpr double binom_pmf(U N, U K, double theta) {
return binom(N, K) * std::pow(theta, K) * std::pow(1 - theta, N - K);
}
这是来自 Clang 10 的错误消息:
/mnt/c/programming/ML/2_3_1_binomial_bernoulli.cpp:12:19: error: static_assert expression is not an integral constant expression
static_assert(N >= K);
^~~~~~
/mnt/c/programming/ML/2_3_1_binomial_bernoulli.cpp:24:12: note: in instantiation of function template specialization 'binom<unsigned long>' requested here
return binom(N, K) * std::pow(theta, K) * std::pow(1 - theta, N - K);
^
/mnt/c/programming/ML/2_3_1_binomial_bernoulli.cpp:36:16: note: in instantiation of function template specialization 'binom_pmf<unsigned long>' requested here
y[K] = binom_pmf(N, K, theta);
问题是static_assert(N >= K)
。
我将参数 N 声明为constexpr size_t
,而 K 是 中的循环参数for (size_t K = 0; K <= N; K++)
,因此任何人都可以得出结论,N 和 K 肯定是 N >= K 的无符号整数,但编译器看起来并不那么高兴。
哦,当我尝试插入static_assert(theta >= 0.0 && theta <= 1.0);
.binom_pdf
我该怎么办?提前致谢。
解决方案
函数参数不是constexpr
。
template <Unsigned U, U N, U K>
constexpr double binom()
将允许您的static_assert
.
推荐阅读
- php - PHP 7.4.8 仅针对特定脚本的最大执行时间
- .net - mgcb 编辑器 (monogame) 无法启动或下载
- javascript - 如何将不带引号的数据从我的网站添加到 Firebase
- azure - 如何从 DataBricks 中删除自动化集群?
- mapbox - 如何旋转 Mapbox 中 geoJSON 指定的图标?
- c++ - 为什么可以在 C++ 中使用没有#include 线程的 std::thread ?
- javascript - 让 React 与生产环境中不同 EC2 实例上的后端 API 服务器对话
- spring-webflux - 有没有办法修改 Spring 创建的默认协程上下文?
- fs - 如何使用 javascript 通过 nexe 访问已编译的 .exe 中的捆绑文件
- javascript - 从导入的 .obj 模型中拖动顶点