Commit 99858df2 authored by Kenton Varda's avatar Kenton Varda Committed by GitHub

Merge pull request #562 from harrishancock/allow-const-one-of-switch

Allow KJ_SWITCH_ONEOF to work on const variables
parents e40cd8d4 08104555
......@@ -133,6 +133,22 @@ TEST(OneOf, Switch) {
}
}
}
{
// At one time this failed to compile.
const auto& constVar = var;
KJ_SWITCH_ONEOF(constVar) {
KJ_CASE_ONEOF(i, int) {
KJ_FAIL_ASSERT("expected char*, got int", i);
}
KJ_CASE_ONEOF(s, const char*) {
KJ_EXPECT(kj::StringPtr(s) == "foo");
}
KJ_CASE_ONEOF(n, float) {
KJ_FAIL_ASSERT("expected char*, got float", n);
}
}
}
}
} // namespace kj
......@@ -128,7 +128,7 @@ public:
typedef _::Variants<sizeof...(Variants)> Tag;
Tag which() {
Tag which() const {
KJ_IREQUIRE(tag != 0, "Can't KJ_SWITCH_ONEOF() on uninitialized value.");
return static_cast<Tag>(tag - 1);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment