I have a function where I want a const std::vector<std::string>*
parameter to have a default parameter value of nullptr
:
// Definition. Includes a default parameter.
void func(std::vector<std::string>* my_strings = nullptr);
// Call site. Is there a better way?
func(&std::vector<std::string>({"abc"}));
Is there a better way to write an optional vector of strings as a parameter?
CodePudding user response:
If func()
doesn't need to modify the vector, and you don't need to differentiate between "no vector" and "an empty vector", you could do it this way:
void func(const std::vector<std::string> & my_strings = std::vector<std::string>());
... then you can just call it naturally:
func(std::vector<std::string>({"abc"}));
func();