How to init default value of string_view with const char

Viewed 1745

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!

1 Answers

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");
Related