0

Let us give any data structure containing objects of std::string_view:

std::vector<std::string_view> v{ "abc", "def" }; std::deque<std::string_view> d{ "abc", "def" }; std::set<std::string_view> s{ "abc", "def" }; 

Is it guaranteed by cpp standard, that these containers store objects of class std::string_view which point to the string literals ended with null?

I mean is it safe to write code like this:

void foo(const char* ptr) { printf("%s", ptr); } for (auto elem : v) foo(elem.data()); for (auto elem : d) foo(elem.data()); for (auto elem : s) foo(elem.data()); 
17
  • 3
    Trying things out is not a good way to determine expected behavior or correctness of code in languages with a concept of "undefined behavior" like C++. @sweenish Commented Nov 3, 2021 at 15:24
  • 2
    No, I don't want C or C++ programmers learning by experimentation. That leads to buggy code and misunderstandings, which cause me no end of headaches. I would rather they consult the standard, a trusted reference material, or ask someone who knows. Commented Nov 3, 2021 at 15:29
  • 1
    Yes, I'm missing what the point is of doing busy work to run an experiment that generates data that is meaningless because it cannot be generalized, if you're going to anyway follow it up by trying to find out for sure. What's the purpose of that? Honestly, this seems more like gate-keeping, as if you expect some minimum "effort" bar for questions on Stack Overflow. That's not the case on this site; we allow questions from beginning to advanced. Although it is generally expected that one do a search first to see if the topic's already been covered. Commented Nov 3, 2021 at 15:31
  • 1
    "Minimal reproducible example" is for debugging questions. This isn't one. There's no threshold for beginner questions, other than that they are on-topic; namely, that they are about a practical programming topic, reasonably scoped such that they can be answered in our Q&A format, not primarily based on opinions, nor soliciting recommendations of off-site resources. "How to Ask" is, of course, always relevant and recommended. It doesn't say that you have to try the code yourself, or set any sort of minimum effort bar, aside from a coherent question. Commented Nov 3, 2021 at 15:35
  • 1
    Yes, closing questions just because they're homework is incorrect. However, assignment dumps are not questions, so, yes, those should obviously be closed as "unclear". Commented Nov 3, 2021 at 15:39

1 Answer 1

2

Yes, this is safe.

In general std::string_views don't have to be null-terminated, but here you explicitly initialized them with null-terminated strings.

The compiler is not allowed to remove null-terminator from a string based solely on the fact that it's assigned to a string_view.

The only case when it would be allowed to do so is if it didn't change the program behavior (the as-if rule), meaning you don't need to worry about it.

Sign up to request clarification or add additional context in comments.

5 Comments

Am I correct that that would not generally work if you constructed a string_view from a std::string?
@SirGuy std::string itself is null-terminated, so that's safe too. OTOH, you're allowed to construct string_view from a part of a string, then there will be no null-terminator.
@SirGuy It depends if you include the end of the string or not. If you dont and try to pass the string view's data as a char* it will ignore where the string_view would normally end and actually end where the later null terminator is.
@HolyBlackCat According to this std::string is not guaranteed to include a null terminator, however the return value of std::string::c_str does. How an implementation achieves that could mean that the null-terminator is always present, but relying on that would not be "guaranteed by the standard"
@SirGuy When converting std::string to std::string_view, std::string_view is constructed from std::string::data(), which is guaranteed to be null-terminated (since C++11), then it'll be fine as long as the std::string's lifetime is long enough.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.