I wanted to be able to do multiple assignments in an if block and short-circuit if the first one fails. However, this does not compile and says, expected primary-expression before ‘auto’
#include <iostream>
#include <optional>
std::optional<int> foo()
{
return 0;
}
int main() {
if (auto a = foo() && auto b = foo())
{
std::cout << "a = " << *a << ", b = " << *b << std::endl;
}
}
The following works though and does what I want.
if (auto a = foo())
{
if (auto b = foo()) {
std::cout << "a = " << *a << ", b = " << *b << std::endl;
}
}
But is there a way for me to use the syntax in the first one? Using parenthesis to surround the expressions does not work.