I have a method
void func(int bar, std::string_view sv = {})
But now I want to set the default value of sv using
const char def = 'X'
How do I achieve this? Thank you!
One way:
const char def = 'X';
void func(int bar, std::string_view sv = {&def, 1});
Note that std::string_view sv = {&def, 1}
produces a std::string_view
to a string with no zero terminator, which may or may not be an issue.
If you need a zero-terminated std::string_view
, then:
std::string_view const def_sv = "X";
void func(int bar, std::string_view sv = def_sv);
Or just:
void func(int bar, std::string_view sv = "X");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With