Excluding raw arrays of basic character types from ranges. #186
Locked
OptimumCpp
started this conversation in
Ideas
Replies: 1 comment
-
|
Hi @OptimumCpp - Beman project uses discourse https://discourse.bemanproject.org/ for discussions. If you'd like restart this on discourse, although I'll admit I don't see how this is on topic. I'm going to lock this thread since we don't use github for this purpose. Thanks. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Raw arrays of basic characters have a dual role in C. They can represent strings as well as ordinary arrays. This ambiguity can result in surprises - dealing with ranges library:
In the above example,
TrySepJupMoonsis a single element range containing the entire input. While the intended behavior wasSepJupMoons. This is a hard to find mistake caused by the ambiguity of raw character arrays. IMO, the proper solution would be to exclude raw character arrays for all basic character types(char,wchar_t...) from thestd::ranges::rangeconcept:This exclusion prevents accidental use of null terminate raw arrays as ranges. L-value raw character arrays can be used as
span(arr)preserving the null charater orbasic_string_view(arr)dropping it. R-value character arrays can use the"str view"svUDL orstd::array{"char arr"}CTAD.It is also possible to define UDLs for
std::arrayto initialize from string literals:Now we can unambiguously handle every case. Normally
""svUDL creates a string view, but we can havestd::arraywith""_0UDL as a null terminated sequence orstd::spanof limited extent for none-null terminated case with""_aUDL.The risk of ambiguous use is eliminated via exclusion in the primary
std::ranges::rangeconcept.PS: the split example is modified sample snippet at cppreference. The modification and shortening is intended to show a problem, that the original carefully and smartly trying to avoid.
Beta Was this translation helpful? Give feedback.
All reactions