Can I make an assumption that given
std::string str;
... // do something to str
Is the following statement is always true?
(str.empty() == (str == ""))
Can I make an assumption that given
std::string str;
... // do something to str
Is the following statement is always true?
(str.empty() == (str == ""))
It should be. The ANSI/ISO standard states in 21.3.3
basic_string
capacity:However, in clause 18 of 21.3.1
basic_string
constructors it states that the character-type assignment operator usestraits::length()
to establish the length of the controlled sequence so you could end up with something strange if you are using a different specialization ofstd::basic_string<>
.I think that the 100% correct statement is that
or something like that. If you haven't done anything strange, then
std::string("")
andstd::string()
should be equivalentThey are logically similar but they are testing for different things.
str.empty()
is checking if the string is empty where the other is checking for equality against a C-style empty string. I would use whichever is more appropriate for what you are trying to do. If you want to know if a string is empty, then usestr.empty()
.