last day (14 days later) » 

7:04 PM
2
A: Why all function in <cstring> must not have constexpr?

OmnifariousUpon reviewing this more, and thinking more about the implications of the C++14 relaxation of rules surrounding constexpr, I have a different answer. The <cstring> header is a wrapper around a bunch of C functions. C has no constexpr concept, and while it might be useful for it to have one, it's...

 
<algorithm>s are not constexpr.
 
@yurikilochek - In the context of the question (asking about D0202R2) they are. That proposal is specifically about making them constexpr.
 
Right, my bad, did not bother to follow the link. Still ::std::find(s, s + largevalue, '\0') - s is UB when s + largevalue is beyond the end of the buffer containing the string. Which basically means you have to know buffer size to use it.
 
@yurikilochek - That's true. Though strlen has a similar problem. :-)
 
How so? It never increments any pointers beyond the \0.
 
7:04 PM
@yurikilochek - And neither does ::std::find. But if there is no '\0'?
 
You do it in s + largevalue, before find is called.
That is already undefined, regardless of '\0' in the buffer.
 
That's true. I still think strlen is one of the more problematic of the <cstring> functions.
 
Or what find does.
 
You could make an unbounded find in C++, and people would scream.
As well they should.
 
Glad that is cleared up. Good thing it is trivial to implement from scratch
 
7:11 PM
I could change my find example to (::std::string(s)).length().
 
No, heap allocation is bad
 
But, that should be constexpr under the new rules I think.
 
::std::string_view(s).length() is ok.
 
nod That makes sense.
 
or just ::std::char_traits<char>::length(s) directly.
A bit more verbose though
 
7:18 PM
::std::char_traits<::std::remove_reference<decltype(*s)>::type>::length(s)
grin I fixed the answer to stop invoking UB with find.
Have you ever heard UB referred to as 'nose demons'?
 

last day (14 days later) »