c++ - 有什么方法可以移动在使用它的结构内返回 std::optional 的 constexpr 函数?
问题描述
我知道问题的标题令人困惑,所以这里是示例:
#include<array>
#include<optional>
#include<type_traits>
static constexpr std::optional<int> maybe_int() {
return {64};
}
struct s {
using type = std::conditional_t< maybe_int().has_value(), std::array<int, *maybe_int()>,float > ;
};
static_assert(std::is_same_v<s::type, std::array<int,64>>);
这段代码编译得很好,但是当我maybe_int
进入 struct 编译失败时。
错误:'static constexpr std::optional<int> s::maybe_int()' 在定义完成之前在常量表达式中调用
请注意,我知道代码可以与自由功能一起正常工作,这是关于可读性的。所以我想maybe_int
在 struct 里面移动。
解决方案
推荐阅读
- neural-network - 为什么使用 cytoscape 在我的网络地图中排除了区域?
- forms - 以 PDF 格式创建具有交互行为的清单
- asp.net-core - FromBody 为空,下一次迁移到 ASP .Net Core MVC 3.1
- django - Django allauth 示例 [Errno 111] - 连接被拒绝
- .net - 在 .NET Core 中引用同一程序集的多个版本?
- javascript - 为什么将函数从父类组件传递给子功能组件不起作用?
- python - 为什么 docker 中的 gunicorn 将我的帖子请求转换为获取请求?
- tensorflow - 张量板嵌入投影仪上的剩余点在哪里,这些点不是从张量板上向下采样的?
- javascript - Javascript Rsocket 自定义元数据 - RSocket 错误 0x201 (APPLICATION_ERROR):没有目标处理程序
- r - 将函数应用于嵌套 r 的列表的每个元素