Is there a safe way to assert if a string view is null terminated?

寵の児 提交于 2019-12-06 07:50:13

It might be a pain, but I would write my own string_view that is guaranteed to be null terminated.

Since a string_view can be constructed like

char array[3] = {'B', 'a', 'r'};
std::string_view array_v(array, sizeof array);

testing

*(sv.data() + sv.length()) == '\0'

is undefined behavior in that case since the last valid index is 2 but you access 3. std::string_view would need to exposes information it can't know in order for you to do this and since it can't, you can't reliably know.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!