[cfe-dev] searching for an empty string in an empty string
mclow.lists at gmail.com
Tue Jun 2 08:32:45 PDT 2015
On Tue, Jun 2, 2015 at 7:51 AM, Tomasz Mikolajczyk <tmmikolajczyk at gmail.com>
> What should be the value of a pos variable in the following piece of code?
> auto pos = std::string().find("");
> I couldn't find answer to that and noticed it varies between standard
> library implementations. For most I've checked pos is 0 (libstdc++, libcxx,
> msvc). I noticed it is npos for Oracle Solaris SunPRO. I think that both
> results are explainable. Is it an UB?
I believe that 0 is the correct answer.
First, a pile of standardese (taken from 184.108.40.206 [string::find]):
size_type find(const charT* s, size_type pos = 0) const;
Requires: s points to an array of at least traits::length(s) + 1 elements
Returns: find(basic_string(s), pos).
size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Effects: Determines the lowest position xpos, if possible, such that both
of the following conditions obtain:
— pos <= xpos and xpos + str.size() <= size();
— traits::eq(at(xpos+I), str.at(I)) for all elements I of the string
controlled by str.
Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.
Remarks: Uses traits::eq().
The first chunk of text says that your call is equivalent to:
The second chuck talks about the results of find(string, pos).
Returning 0 fulfills the first condition: pos <= xpos and xpos + str.size()
Returning 0 also fulfills the second condition, because there are no
"elements of the string controlled by str"
P.S. If I were you, I would file a bug against Oracle Solaris SunPRO.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the cfe-dev