I want to pass a std::list as a parameter to fn(std::list<int>), so I do fn({10, 21, 30}) and everybody is happy.
However, I've come to learn that one shouldn't pass list by value, cause it's costly. So, I redefine my fn as fn(std::list<int> &). Now, when I do the call fn({10, 21, 30}), I get an error: candidate function not viable: cannot convert initializer list argument to 'std::list<int> &'.
QUESTION TIME
- Is the "you shall not pass an costly object by value" rule valid here? We aren't passing a
listafter all, but aninitializer_list, no? - If the rule still applies, what's the easy fix here?
I guess my doubt comes from the fact that I don't know clearly what happens when one passes an initializer_list argument to a function that accepts a list.
- Is
listgenerated on the spot and then passed by value? If not, what is it that actually happens?
Aucun commentaire:
Enregistrer un commentaire