c++ - 用另一个布尔值提升可选
问题描述
我正在尝试编译类似的东西:
struct Obj { int i = 100; };
boost::optional<Obj> f(const bool _b)
{
if (_b) return Obj(); else return boost::none;
}
int main()
{
bool run = true;
while (boost::optional<Obj> retVal = f(true) && run)
{
std::cout << retVal.i;
}
return 0;
}
是否有可能实现它 - 将有效对象存储在变量中并将其分解为布尔值?
解决方案
您可以将您的更改while
为for
:
for (boost::optional<Obj> retVal = f(true), retVal && run; retVal = f(true))
{
std::cout << retVal.i;
}
推荐阅读
- python - Python 3 - 问题导入模块 - 从父目录 - 包含文件描述符
- reactjs - 单击按钮时加载并播放另一首歌曲
- java - 多语言切换跳转后我的设备会闪回后台
- jenkins - 如何在 Jenkins 种子作业中获取 DSL 脚本中的参数
- php - 使用 laravel 5.5 播种 faker 命令
- visual-studio-2015 - SSIS 不显示数据预览
- python - 设置未知 Django 模型字段的值
- rubygems - VSTS Build Pipeline:捆绑安装任务失败
- java - 带有路径变量的 RestTemplate GET 方法
- polymer - Polymer 3 升级 - 选择的铁页在 dom 中但不显示