0

Is it safe to set an empty default value for the std::string_view?

void func(std::string_view arg = {} )
Fareanor
  • 5,900
  • 2
  • 11
  • 37
Hardwired
  • 804
  • 1
  • 8
  • 19

1 Answers1

1

It's allowed to create an empty std::string_view the same way it's allowed to create an empty std::string for example.

Of course, if your function intends to do some direct access operations (for example std::string_view::front() or std::string_view::back()), in that case, it will be necessary to check if the std::string_view is not empty beforehand (std::string_view::empty()).

Fareanor
  • 5,900
  • 2
  • 11
  • 37