I would like to know how to properly handle a string input in a function, if I know I will have to make a copy inside of it to place it inside a container.
I was thinking of doing it like this:
void foo(const string& s){
container.push(s);
}
But if I pass a char* string I will make 2 copies, first when calling foo, then when passing it to a container.
My second idea was to use a string_view, as it is often said, that if you can use a view, you should. So my second version of the function looked like this:
void foo2(string_view s){
container.push(string(s));
}
Ok, now I will make only a single copy no matter what type of string I am given. But then I started thinking, why couldn't I just accept a string as an argument to a function, simplifying it like this:
void foo3(string s){
container.push(std::move(s));
}
But now I have to make sure, that my container properly utilizes move semantics so it doesn't end up making another copy! As this container object is a templated type it means using perfect forwarding and so on, which is whole lot of work in itself.
At this point I have no idea which option to choose, so I would like to ask you for some advice. I also feel like I am way overthinking this, as this probably won't have that much of a performance impact in the end, but I would like to do it right. Thanks in advance.