The following code snippet returns always true on my compiler(visual studio). But is this behavior well defined and portable?
bool return_always_true(std::string const& str)
{
return str.find("") != std::string::npos;
}
int main(){
cout << boolapha << return_always_true("") << endl
<< return_always_true("oxylottl") << endl
<< return_always_true("lorem ipsum") << endl;
//true true true
}
I find cppreference.com easier to read than the standard. Quoting them:
Finds the first substring equal to
str
...Formally, a substring
str
is said to be found at positionxpos
if all of the following is true:
xpos >= pos
xpos + str.size() <= size()
- for all positions
n
instr
,Traits::eq(at(xpos+n), str.at(n))
An empty string will always match at the start of the target string because
str
so the match condition is vacuously true.Yes it is: "an empty substring is found at pos if and only if pos <= size()"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With