fix: don't force a copy of std::string fname when moving is an option

Taking this parameter by const reference forces us to copy it (because
we know we're going to store it). Taking it by r-value reference would
suggest that we _might_ take ownership over it and would also force the
user to make a copy if they wish to retain the original value.

Taking this parameter by value however clearly gives us ownership of its
content without forcing a copy if it's implicit conversion from
`const char*` or explicitly handed over to us by the user via std::move.
This commit is contained in:
Giel van Schijndel
2022-06-02 14:39:10 +02:00
parent 25be97dc39
commit aff6f0f574

View File

@@ -2317,7 +2317,7 @@ template<typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY,
template<typename ...> class Table = std::unordered_map,
template<typename ...> class Array = std::vector>
basic_value<Comment, Table, Array>
parse(std::istream& is, const std::string& fname = "unknown file")
parse(std::istream& is, std::string fname = "unknown file")
{
using value_type = basic_value<Comment, Table, Array>;
@@ -2372,14 +2372,14 @@ parse(std::istream& is, const std::string& fname = "unknown file")
template<typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY,
template<typename ...> class Table = std::unordered_map,
template<typename ...> class Array = std::vector>
basic_value<Comment, Table, Array> parse(const std::string& fname)
basic_value<Comment, Table, Array> parse(std::string fname)
{
std::ifstream ifs(fname.c_str(), std::ios_base::binary);
if(!ifs.good())
{
throw std::runtime_error("toml::parse: file open error -> " + fname);
}
return parse<Comment, Table, Array>(ifs, fname);
return parse<Comment, Table, Array>(ifs, std::move(fname));
}
#ifdef TOML11_HAS_STD_FILESYSTEM