c++ - 在编译时生成函数
问题描述
我正在尝试使用 boost hana 在编译时生成函数。这是我写的代码
#include <boost/hana/transform.hpp>
#include <array>
template<int i>
double f(double x)
{
return x * i;
}
int main()
{
constexpr std::array arr = {1,5,10,100,500};
constexpr auto functions = hana::transform(arr,
[](const int a) -> double (*)(double)
{
return f<a>;
}
);
}
编译时出现 f 不能转换为 double (*)(double) 类型的错误。
我认为问题在于 a 不是 constexpr (这是不可能的,因为它是一个函数参数)。有没有办法让它工作?
解决方案
有没有办法让它工作?
不是这样。
看看你的 lambda
[](const int a) -> double (*)(double)
{
return f<a>;
}
您正在使用参数a
,它在 lambda 中可能是已知的运行时,作为模板参数,它必须是已知的编译类型。
不能工作。
推荐阅读
- selenium-chromedriver - TestNG - Chrome 版本 79 的 ChromeDriver - 防止恶意代码访问
- ruby - 在 Thread Ruby 中访问模块
- linq - Linq Query Select from table1 where id in (select from table2 inner join table3 on table2 = table3 where table3 = id)
- angular - Angular - 根据另一个对象数组中的值过滤对象数组
- python - Pypy 在 Raspberry Pi 上找不到 Flask
- azure-devops - 创建 YAML 后如何创建构建完成触发器
- r - ckanr 函数 ds_search_sql 或替代
- html - 将元素放入容器的问题
- android - Flutter中成功的firebase身份验证发生错误
- python - BeautifulSoup --- if / else --- 检查容器是否包含另一个容器