c++stringc++14user-defined-literals

Advantages of using user-defined literal for strings instead of string literal


The strings topic in the SO Documentation used to say, in the Remarks section:

Since C++14, instead of using "foo", it is recommended to use "foo"s, as s is a string literal, which converts the const char * "foo" to std::string "foo".

The only advantage I see using

std::string str = "foo"s;

instead of

std::string str = "foo";

is that in the first case the compiler can perform copy-elision (I think), which would be faster than the constructor call in the second case.

Nonetheless, this is (not yet) guaranteed, so the first one might also call a constructor, the copy constructor.

Ignoring cases where it is required to use std::string literals like

std::string str = "Hello "s + "World!"s;

Is there any benefit of using std::string literals instead of const char[] literals?


Solution

  • If you're part of the "Almost Always Auto" crowd, then the UDL is very important. It lets you do this:

    auto str = "Foo"s;
    

    And thus, str will be a genuine std::string, not a const char*. It therefore permits you to decide when to do which.

    This is also important for auto return type deduction:

    []() {return "Foo"s;}
    

    Or any form of type deduction, really:

    template<typename T>
    void foo(T &&t) {...}
    
    foo("Foo"s);
    

    The only advantage I see using [...] instead of [...] is that in the first case the compiler can perform copy-elision (I think), which would be faster than the constructor call in the second case.

    Copy-elision is not faster than the constructor call. Either way, you're calling one of the object's constructors. The question is which one:

    std::string str = "foo";
    

    This will provoke a call to the constructor of std::string which takes a const char*. But since std::string has to copy the string into its own storage, it must get the length of the string to do so. And since it doesn't know the length, this constructor is forced to use strlen to get it (technically, char_traits<char>::length, but that's probably not going to be much faster).

    By contrast:

    std::string str = "foo"s;
    

    This will use the UDL template that has this prototype:

    string operator "" s(const char* str, size_t len);
    

    See, the compiler knows the length of a string literal. So the UDL code is passed a pointer to the string and a size. And thus, it can call the std::string constructor that takes a const char* and a size_t. So there's no need for computing the string's length.

    The advice in question is not for you to go around and convert every use of a literal into the s version. If you're fine with the limitations of an array of chars, use it. The advice is that, if you're going to store that literal in a std::string, it's best to get that done while it's still a literal and not a nebulous const char*.