std::string str = "Hello, world!"; StringRef ref = str.substr(0, 5); // creates a reference to "Hello" assert(str == "Hello, world!"); // original string is unchanged
void processString(StringRef str) { for (char c : str) { // do something with each character in the string } } int main() { std::string str = "abcdefg"; processString(str); // no copies are made return 0; }The StringRef class is part of the LLVM/Clang library, which can be used in C++ programs by including the appropriate headers and linking against the library.