Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ string that can be NULL

Tags:

c++

string

null

I'm used to passing around string like this in my C++ applications:

void foo(const std::string& input)
{
  std::cout << input.size() << std::endl;
}

void bar()
{
  foo("stackoverflow");
}

Now I have a case where I want the string to be NULL:

void baz()
{
  foo("stackoverflow");
  foo(NULL); // very bad with foo implementation above
}

I could change foo to:

void foo(const std::string* input)
{
  // TODO: support NULL input
  std::cout << input->size() << std::endl;
}

But to pass a string literal or copy a char* to that implementation of foo I need to write something like this:

void bar()
{
  string input("hi"); // annoying temporary
  foo(&input);
  foo(NULL);  // will work as long as foo handles NULL properly
}

I started thinking about inheriting from std::string and adding a null property, but I'm not so sure it's a good idea. Maybe it is better to simply use a const char* string for parameters that can be NULL, but what if I want to save a copy of the string (or NULL) without having to manage its memory myself? (See What are some of the drawbacks to using C-style strings? etc.)

Any clever solution around?

like image 488
activout.se Avatar asked Dec 04 '08 19:12

activout.se


People also ask

What can be null in C?

The C and C++ languages have a null character (NUL), a null pointer (NULL), and a null statement (just a semicolon (;)). The C NUL is a single character that compares equal to 0. The C NULL is a special reserved pointer value that does not point to any valid data object.

Can a string value be null?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .

What is the null character in C strings?

'\0' is defined to be a null character. It is a character with all bits set to zero. This has nothing to do with pointers. '\0' is (like all character literals) an integer constant with the value zero.

Is empty string same as null in C?

The value null represents the absence of any object, while the empty string is an object of type String with zero characters. If you try to compare the two, they are not the same.


2 Answers

If you want the type to be null, then make it a pointer. Pass string pointers around instead of references, since this is precisely what pointers can do, and references cant. References always point to the same valid object. Pointers can be set to null, or be reseated to point to another object. Thus, if you need the things pointers can do, use pointers.

Alternatively, use boost::optional, which allows a more type-safe way to specify "this variable may or may not contain a value".

Or, of course, change the semantics so you either use empty strings instead of null, pass a separate bool parameter specifying whether the string is available or not, or refactor so you don't need this in the first place.

like image 139
jalf Avatar answered Oct 28 '22 15:10

jalf


Function overloading to the rescue...

void foo( const std::string& input )
{
    std::cout << input << std::endl;

    // do more things ...
}

void foo( const char* input )
{
    if ( input != NULL ) foo( std::string(input) );
}

This will accept both c-style char arrays and std::strings, and will incur extra overhead on the stack if you pass in a string literal or a char array, but allows you to keep your implementation in one place and keep your nice syntax.

like image 31
eplawless Avatar answered Oct 28 '22 16:10

eplawless