Avoid unnecessary copies of parser result

The 'result' class has unwrap() and unwrap_err() member functions
overloaded for const lvalue and rvalue *this to avoid an unnecessarily
copying the to-be unwrapped object of its containing object is going to
be discarded anyway.  Alas, the parse() function toml/parser.hpp file
stored the parse result in a local `const` variable so, although the
unwrap call would have been the last use of the object in each case, the
unnecessary copy would still be made.  This patch removes the `const`
and adds a std::move() to actually benefit from the already implemented
optimization.
This commit is contained in:
Moritz Klammler 2022-09-15 21:41:13 +02:00
parent e86d7c3cd3
commit e064a5c371

View File

@ -2414,12 +2414,14 @@ parse(std::vector<char>& letters, const std::string& fname)
}
}
const auto data = detail::parse_toml_file<value_type>(loc);
if(!data)
if (auto data = detail::parse_toml_file<value_type>(loc))
{
throw syntax_error(data.unwrap_err(), source_location(loc));
return std::move(data).unwrap();
}
else
{
throw syntax_error(std::move(data).unwrap_err(), source_location(loc));
}
return data.unwrap();
}
} // detail