I'm new here and this is my first question.
So, I have this function:
std::string join(string_initializer_list p_input) const {
std::string output;
for (auto const & s : p_input) {
output.append(s);
}
return output;
}
The main objective of that function is to join a list of strings returning a new string.
For the parameter I've used a std::initializer_list<std::string> aliased as string_initializer_list.
The parameter is passed by value because after doing a bit of research, I noticed that I was just passing pointers around and that this was the most idiomatic and correct way to do so.
My question has to do with the for loop: which is more correct in this case and why? Should I use auto const & or auto &&?
The thing is: I don't want to change any of the strings from the initializer_list, and I want to leave that clear. From that perspective const & seems more correct but I would to know your opinions about this matter.
After doing some research, testing it in C++ and reading lots of questions here, I don't yet fully understand how auto && works.
Also, in a first draft I also had the parameter being passed as && as a way to prevent it from being an lvalue.
I intend only to use this function for quick stuff like this:
join({"Hello, ", "world"});
In rare ocasions, one of those std::strings will end up being an lvalue:
join({"Hello, ", some_string});
The main concept here is to make a cheap and easy way of concatenating strings. This is another function I made, similar with the one above but for a different context.
std::string & concatenate(std::string & p_output, string_initializer_list p_input) const {
for (auto const & s : p_input) {
p_output.append(s);
}
return p_output;
}
Once more, the perfectionist in me is asking about that for loop.