How Many Strings Does C++ Have?

An amusing enumeration of several string options available in C++.

(…OK, a language lawyer would nitpick suggesting “How many string types…”, but I wanted a catchier title.)

So, if you program in Python and you see something enclosed in double quotes, you have a string:

s = "Connie"

Something similar happens in Java, with string literals like “Connie” implemented as instances of the java.lang.String class:

String s = "Connie";

All right.

Now, let’s enter – drumroll, please – The Realm of C++! And the fun begins 🙂

So, let’s consider this simple line of C++ code:

auto s1 = "Connie";

What is the type of s1?

std::string? A char[7] array? (Hey, “Connie” is six characters, but don’t forget the terminating NUL!)

…Something else?

So, you can use your favorite IDE, and hover over the variable name, and get the deduced type. Visual Studio C++ IntelliSense suggests it’s “const char*”. Wow!

Visual Studio IntelliSense deduces const char pointer.
Visual Studio IntelliSense deduces const char pointer

And what about “Connie”s?

auto s2 = "Connie"s;

No, it’s not the plural of “Connie”. And it’s not a malformed Saxon genitive either. This time s2 is of type std::string! Thank you operator””s introduced in C++14!

Visual Studio IntelliSense deduces std::string
Visual Studio IntelliSense deduces std::string

But, are we done? Of course, not! Don’t forget: It’s C++! 🙂

For example, you can have u8“Connie”, which represents a UTF-8 literal. And, of course, we need a thread on StackOverflow to figure out “How are u8-literals supposed to work?”

And don’t forget L“Connie”, u“Connie” and U“Connie”, which represent const wchar_t*, const char16_t* (UTF-16 encoded) and const char32_t* (UTF-32 encoded) respectively.

Now we are done, right? Not yet!

In fact, you can combine the previous prefixes with the standard s-suffix, for example: L“Connie”s is a std::wstring! U“Connie”s is a std::u32string. And so on.

Done, right? Not yet!! In fact, there are raw string literals to consider, too. For example: R”(C:\Path\To\Connie)”, which is a const char* to “C:\Path\To\Connie” (well, this saves you escaping \ with \\).

And don’t forget the combinations of raw string literals with the above prefixes and optionally the standard s-suffix, as well: LR”(C:\Path\To\Connie)”UR”(C:\Path\To\Connie)”LR”(C:\Path\To\Connie)”sUR”(C:\Path\To\Connie)”s, and more!

Oh, and in addition to the standard std::string class, and other standard std::basic_string-based typedefs (e.g. std::wstring, std::u16string, std::u32string, etc.), there are platform/library specific string classes, like ATL/MFC’s CString, CStringA and CStringW. And Qt brings QString to the table. And wxWidgets does the same with its wxString.

Wow! And I would not be surprised if I missed some other string variation out 🙂

7 thoughts on “How Many Strings Does C++ Have?”

  1. The ROOT particle physics analysis library/package implements its own TString because it originated before the C++ Standard Library was in common use.

    Like

  2. Indeed there is also the std::string_view template family which despite being not technically a string, is something you have to consider when designing interfaces that accept string.
    Great article!

    Like

    1. Thanks! I do know about string views, of course. The options enumerated in this article were by no means meant to be fully exhaustive, as I also stated in the article. There are also other Windows-specific string options, like COM strings (BSTR) and their C++ ATL wrapper CComBSTR. I may write a continuation of this article in the future.

      Like

Leave a comment