<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Jun 2, 2015 at 7:51 AM, Tomasz Mikolajczyk <span dir="ltr"><<a href="mailto:tmmikolajczyk@gmail.com" target="_blank">tmmikolajczyk@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Hi,<div><br>What should be the value of a pos variable in the following piece of code?<br><br>auto pos = std::string().find("");<br><br></div><div>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?</div><div><br></div></div></blockquote><div><br></div><div>I believe that 0 is the correct answer.</div><div><br></div>







<div>First, a pile of standardese (taken from 21.4.4.7 [string::find]):</div><div><br></div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">size_type find(const charT* s, size_type pos = 0) const;</span></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">
</span></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">Requires: s points to an array of at least traits::length(s) + 1 elements of charT. </span></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">Returns: find(basic_string(s), pos).</span></div></div></div></div></blockquote><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">
and then:

</span></div></div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">size_type find(const basic_string& str, size_type pos = 0) const noexcept;</span></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">Effects: Determines the lowest position xpos, if possible, such that both of the following conditions obtain:</span></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap"> — pos <= xpos and xpos + str.size() <= size();</span></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">  — traits::eq(at(xpos+I), <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__str.at&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=c5sS96S2WyfMb4Cxya8oHeFdedtzYTy4VIW0LuQVB38&s=jqwJkUcA0JVlRbYazq0bts7tbMFNHKCgHn5SEL-qXb8&e=" target="_blank">str.at</a>(I)) for all elements I of the string controlled by str.</span></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">
</span></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. </span></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><span style="white-space:pre-wrap">Remarks: Uses traits::eq().</span></div></div></div></div></blockquote><div class="gmail_extra"><div class="gmail_quote"><div><span style="white-space:pre-wrap"><br></span></div><div>The first chunk of text says that your call is equivalent to:</div><div>     std::string().find(string(""), 0);</div><div><br></div><div>The second chuck talks about the results of find(string, pos).</div><div>Returning 0 fulfills the first condition: <span style="white-space:pre-wrap">pos <= xpos and xpos + str.size() <= size()</span></div><div><span style="white-space:pre-wrap">Returning 0 also fulfills the second condition, because there are no "elements of the string controlled by str"</span></div><div><span style="white-space:pre-wrap"><br></span></div><div><span style="white-space:pre-wrap">-- Marshall</span></div><div><span style="white-space:pre-wrap"><br></span></div><div><span style="white-space:pre-wrap">P.S.  If I were you, I would file a bug against </span>Oracle Solaris SunPRO.</div><div><span style="white-space:pre-wrap"><br></span></div></div></div></div>