In my C++ app I'm concatenating a few strings using the +
operator as follows in a reproducible example:
#include <string>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <sstream>
std::string download_file_contents(const std::string& server_url)
{
const curlpp::options::Url url(server_url);
curlpp::Easy request;
request.setOpt(url);
std::ostringstream output_stream;
const curlpp::options::WriteStream write_stream(&output_stream);
request.setOpt(write_stream);
request.perform();
return output_stream.str();
}
const std::string server_files_url = "https://example.com";
int main()
{
for (const std::map<std::string, std::string> files = { {"a", "b" }, { "c", "d" }};
const auto& [key, value] : files)
{
try
{
const auto file_download_url = server_files_url + "/?" + key; // Warning occurs here at "+ key"
const auto file_contents = download_file_contents(file_download_url);
std::cout << key << " downloaded, value: " << value << std::endl;
}
catch (const std::exception& exception)
{
std::cerr << "Exception: " << exception.what() << std::endl;
}
}
}
The string concatentation code is called in a for loop. Using string.append()
doesn't seem to make sense here since I need to make a new variable for e.g. the file_download_url
for each iteration: Modifying the server_files_url
is not semantically correct. Why do I still get the clang warning performance-inefficient-string-concatenation
? I do not really have a performance problem from using the +
operator twice in a single line of code per loop iteration. I also believe the MSVC compiler would be smart enough to optimize the string concatentation as much as possible in release mode to avoid any potential performance hit. I guess clang-tidy
does not care about potential compiler optimizations being performed or not.
Any idea how to properly rewrite this code to avoid the warning?