C++ boost可选,使用另一个布尔值

C++ boost可选,使用另一个布尔值,c++,boost,boost-optional,C++,Boost,Boost Optional,我正在尝试编译类似以下内容: 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)

我正在尝试编译类似以下内容:

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;
}
结构对象{int i=100;}; boost::可选f(常数布尔b) { if(_b)返回Obj();else返回boost::none; } int main() { bool run=true; while(boost::optional retVal=f(true)&&run) {
std::cout您可以将
while
更改为
for
,如:

for (boost::optional<Obj> retVal = f(true), retVal && run; retVal = f(true))
{
    std::cout << retVal.i;
}
for(boost::可选retVal=f(true),retVal&&run;retVal=f(true))
{
标准::cout