mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:07:36 +00:00
AK: Add rvalue-ref qualifiers for Optional's value() and value_or()
This avoids a value copy when calling value() or value_or() on a temporary Optional. This is very common when using the HashMap::get() API like this: auto value = hash_map.get(key).value_or(fallback_value);
This commit is contained in:
parent
0b36499f46
commit
7dda773426
2 changed files with 36 additions and 3 deletions
|
@ -34,6 +34,27 @@ TEST_CASE(move_optional)
|
|||
EXPECT_EQ(x.has_value(), false);
|
||||
}
|
||||
|
||||
TEST_CASE(optional_rvalue_ref_qualified_getters)
|
||||
{
|
||||
struct DontCopyMe {
|
||||
DontCopyMe() { }
|
||||
~DontCopyMe() = default;
|
||||
DontCopyMe(DontCopyMe&&) = default;
|
||||
DontCopyMe& operator=(DontCopyMe&&) = default;
|
||||
DontCopyMe(DontCopyMe const&) = delete;
|
||||
DontCopyMe& operator=(DontCopyMe const&) = delete;
|
||||
|
||||
int x { 13 };
|
||||
};
|
||||
|
||||
auto make_an_optional = []() -> Optional<DontCopyMe> {
|
||||
return DontCopyMe {};
|
||||
};
|
||||
|
||||
EXPECT_EQ(make_an_optional().value().x, 13);
|
||||
EXPECT_EQ(make_an_optional().value_or(DontCopyMe {}).x, 13);
|
||||
}
|
||||
|
||||
TEST_CASE(optional_leak_1)
|
||||
{
|
||||
struct Structure {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue