tags:

views:

106

answers:

6

Hi, If using a char*, I can initialize it to NULL and later check if it is set by doing a comparison. How to do the same thing for a std::string? How to check if the string is set or not?

EDIT: What if the string I set to is also empty? Do I have to use an additional flag to check if the std::string is set or not?

+1  A: 

Use empty():

std::string s;

if (s.empty())
    // nothing in s
GMan
What happens if the string is `""`
nakiya
@nakiya: That's empty. `std::string` isn't a pointer, you can't assign null to it.
GMan
This works ok for me... `std::string s = ""`
nakiya
@nakiya: As it should.
GMan
So basically if the string I assign can be anything including `""`, I have to keep another `bool` or something to check if it has been set. :S
nakiya
@nakiya: Either that or use a pointer to a `std::string`, as Michael Burr suggests. Or use a wrapper that provides such functionality (like Boost.Optional).
James McNellis
@nakiya: It's pretty hard to answer a question with no context.
GMan
@GMan: You apparently did a good job answering it though :-P
James McNellis
@James: Heh, yeah. I was going to delete it, but I guess not now. +1 to you and Michael though.
GMan
+1  A: 

The default constructor for std::string always returns an object that is set to a null string.

verisimilidude
To be clear, the default constructor returns an empty string, `std::string() == std::string("")`
TokenMacGuy
+4  A: 

You can't; at least not the same way you can test whether a pointer is NULL.

A std::string object is always initialized and always contains a string; its contents by default are an empty string ("").

You can test for emptiness (using s.size() == 0 or s.empty()).

James McNellis
A: 

There is no "unset" state for std::string, it is always set to something.

Employed Russian
A: 

I don't think you can tell with the std::string class. However, if you really need this information, you could always derive a class from std::string and give the derived class the ability to tell if it had been changed since construction (or some other arbitrary time). Or better yet, just write a new class that wraps std::string since deriving from std::string may not be a good idea given the lack of a base class virtual destructor. That's probably more work, but more work tends to be needed for an optimal solution.

Of course, you can always just assume if it contains something other than "" then it has been "set", this won't detect it manually getting set to "" though.

PatrickV
It's generally a bad idea to derive publicly from a standard library container because they don't have virtual destructors. It would be better to encapsulate a `std::string` as a member variable.
James McNellis
@James McNellis: I can see that point. I'll incorporate that into the answer.
PatrickV
+8  A: 

As several answers pointed out, std::string has no concept of 'nullness' for its value. If using the empty string as such a value isn't good enough (ie., you need to distinguish between a string that has no characters and a string that has no value), you can use a std::string* and set it to NULL or to a valid std::string instance as appropriate.

You may want to use some sort of smart pointer type (boost::scoped_ptr or something) to help manage the lifetime of any std::string object that you set the pointer to.

Michael Burr
You can also use `boost::optional<std::string>`. The template `boost::optional<T>` exists precisely to support "null-ness" for types that don't have it naturally.
MSalters