Initialization of const
objects can be delayed using ternary operator or IILE:
std::vector<byte> dumpedGamePak ; const std::size_t CGB_support_code = 0x0143;
enum class console : uint8 { dmg, cgb_compatible, cgb_only };
const console m_type = dumpedGamePak[CGB_support_code] == 0xC0 ? console::cgb_only : dumpedGamePak[CGB_support_code] == 0x80 ? console::cgb_compatible : console::dmg;
|
Ternary operator trick is available since C89. A more relaxed approach is using IILE:
Stmt Parser::forStatement() { consume(TokenKind::LeftParen, "Expect '(' after 'for'.");
const Stmt initializer = [&]() -> Stmt { if (match(TokenKind::Semicolon)) return boost::blank{};
if (match(TokenKind::Var)) return variableDeclaration();
return expressionStatement(); }();
}
|