1

I am trying to compile something akin to this:

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

Is it possible to achieve it at all - to store a valid object in a variable and have it decomposed to a bool too?

Anupam Srivastava
  • 789
  • 1
  • 8
  • 25

1 Answers1

1

You could change your while into a for like:

for (boost::optional<Obj> retVal = f(true), retVal && run; retVal = f(true))
{
    std::cout << retVal.i;
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402