Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to init default value of string_view with const char

Tags:

c++

c++17

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!

like image 964
resnet Avatar asked Jul 02 '19 10:07

resnet


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");
like image 112
Maxim Egorushkin Avatar answered Sep 27 '22 01:09

Maxim Egorushkin