1

我正在尝试编译类似的东西:

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;
}

是否有可能实现它 - 将有效对象存储在变量中并将其分解为布尔值?

4

1 回答 1

1

您可以将您的更改whilefor

for (boost::optional<Obj> retVal = f(true), retVal && run; retVal = f(true))
{
    std::cout << retVal.i;
}
于 2020-03-18T14:31:51.323 回答