Why can't a string_view be used as a condition expression?

3 days ago 5
ARTICLE AD BOX

The possibility was considered on a more general basis (so not just string_view, but also string, etc.) and voted on. At the Bristol meeting, the vote was:

strongly favor: 0 weakly favor: 1 weakly oppose: 3 Strongly oppose: 5

The record shows the vote, but not the discussion that took place.

When string_view was proposed, the paper included a section titled "why not change?" and listed a number things that people might consider doing differently, including: "Why not add an explicit operator bool?" The answer to that referred to the earlier vote on the proposal to add an operator bool to containers more generally (which led to the vote cited above).

In the previous (4th) revision of the string_view proposal, the author addressed the possibility of an explicit operator bool() as follows:

This would be an abbreviation for !empty(), usable for initialization in if statements. I didn't add this because it would be inconsistent with the rest of the containers library, but if another proposal adds it to the rest of the containers, that proposal should also add it to string_view.

So it appears that the history is:

the string_view proposal said they're not including it because doing so would be inconsistent with other containers, but if it was added to other containers, it should also be added to string_view. A proposal to add it to other containers was rejected The author didn't see string_view as enough different from other containers to justify including it in string_view when it's absent from (for example) string.

It's possible that when the rev 5 proposal for string_view was presented that there was discussion of string_view being enough different from other containers to justify its having an explicit operator bool(), even though others don't. But if so, there seems to be no record of that discussion. Unless somebody who was present remembers (and posts) about a discussion, I'd guess no such thing happened; they simply accepted the explanation given.

string_view proposal, rev 5:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3762.html#operator-bool string_view proposal, rev 4:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3685.html#operator-bool Vote:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3762.html#operator-bool
Read Entire Article