From aff6f0f57469f43945833a17dc8276c5c4235638 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Thu, 2 Jun 2022 14:39:10 +0200 Subject: [PATCH] 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. --- toml/parser.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/toml/parser.hpp b/toml/parser.hpp index e51708e..6784181 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -2317,7 +2317,7 @@ template class Table = std::unordered_map, template class Array = std::vector> basic_value -parse(std::istream& is, const std::string& fname = "unknown file") +parse(std::istream& is, std::string fname = "unknown file") { using value_type = basic_value; @@ -2372,14 +2372,14 @@ parse(std::istream& is, const std::string& fname = "unknown file") template class Table = std::unordered_map, template class Array = std::vector> -basic_value parse(const std::string& fname) +basic_value 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(ifs, fname); + return parse(ifs, std::move(fname)); } #ifdef TOML11_HAS_STD_FILESYSTEM