Initializing strings as null vs empty string

C++StringInitialization

C++ Problem Overview


How would it matter if my C++ code (as shown below) has a string initialized as an empty string :

std::string myStr = "";
....some code to optionally populate 'myStr'...
if (myStr != "") {
    // do something
}

vs. no/null initialization:

std::string myStr;
....some code to optionally populate 'myStr'...
if (myStr != NULL) {
    // do something
}

Are there any best practices or gotchas around this?

C++ Solutions


Solution 1 - C++

There's a function empty() ready for you in std::string:

std::string a;
if(a.empty())
{
	//do stuff. You will enter this block if the string is declared like this
}

or

std::string a;
if(!a.empty())
{
	//You will not enter this block now
}
a = "42";
if(!a.empty())
{
	//And now you will enter this block.
}

Solution 2 - C++

There are no gotchas. The default construction of std::string is "". But you cannot compare a string to NULL. The closest you can get is to check whether the string is empty or not, using the std::string::empty method..

Solution 3 - C++

Best:

 std::string subCondition;

This creates an empty string.

This:

std::string myStr = "";

does a copy initialization - creates a temporary string from "", and then uses the copy constructor to create myStr.

Bonus:

std::string myStr("");

does a direct initialization and uses the string(const char*) constructor.

To check if a string is empty, just use empty().

Solution 4 - C++

I would prefere

if (!myStr.empty())
{
    //do something
}

Also you don't have to write std::string a = "";. You can just write std::string a; - it will be empty by default

Solution 5 - C++

Empty-ness and "NULL-ness" are two different concepts. As others mentioned the former can be achieved via std::string::empty(), the latter can be achieved with boost::optional<std::string>, e.g.:

boost::optional<string> myStr;
if (myStr) { // myStr != NULL
    // ...
}

Solution 6 - C++

The default constructor initializes the string to the empty string. This is the more economic way of saying the same thing.

However, the comparison to NULL stinks. That is an older syntax still in common use that means something else; a null pointer. It means that there is no string around.

If you want to check whether a string (that does exist) is empty, use the empty method instead:

if (myStr.empty()) ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaketView Question on Stackoverflow
Solution 1 - C++SingerOfTheFallView Answer on Stackoverflow
Solution 2 - C++juanchopanzaView Answer on Stackoverflow
Solution 3 - C++Luchian GrigoreView Answer on Stackoverflow
Solution 4 - C++AndrewView Answer on Stackoverflow
Solution 5 - C++Adam RomanekView Answer on Stackoverflow
Solution 6 - C++Jirka HanikaView Answer on Stackoverflow