24

My question is related to the use of the "s" suffix in C++?

Example of code using the "s" suffix:

auto hello = "Hello!"s; // a std::string

The same could be written as:

auto hello = std::string{"Hello!"};

I was able to find online that the "s" suffix should be used to minimizes mistakes and to clarify our intentions in the code.

Therefore, is the use of the "s" suffix only meant for the reader of the code? Or are there others advantages to its use?

Rpessoa
  • 361
  • 1
  • 3
  • 6
  • i wonder if this addresses an old concern of mine https://stackoverflow.com/questions/2312860/correct-idiom-for-stdstring-constants – pm100 Jan 26 '18 at 16:10
  • The example on this page may shows at least 1 advantage: http://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s – tomalbrc Jan 26 '18 at 16:11
  • 2
    Better to avoid using `auto` in such cases than use such weird workarounds – Anton Malyshev Jan 26 '18 at 16:11
  • @Rpessoa - you would be better off without 'auto' in your sample. It is confusing readers of the question – pm100 Jan 26 '18 at 16:15
  • @MichaelWalz: you miss a `const` Btw: `const char* hello = "Hello";` – Jarod42 Jan 26 '18 at 16:41
  • 1
    @pm100 - I used the auto in purpose. If I had `std::string hello{"Hello"};` or `std::string hello = "hello";` why would I need to use the "s" suffix? I already included the std::string, so it would be clear that I wanted a string. Maybe I am missing some of the use of the "s" suffix. Just as a clarification, how would you re-write my code example? – Rpessoa Jan 26 '18 at 17:32

2 Answers2

34

Null characters can be included in the raw string trivially; example from http://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s

int main()
{
    using namespace std::string_literals;

    std::string s1 = "abc\0\0def";
    std::string s2 = "abc\0\0def"s;
    std::cout << "s1: " << s1.size() << " \"" << s1 << "\"\n";
    std::cout << "s2: " << s2.size() << " \"" << s2 << "\"\n";
}

Possible output:

s1: 3 "abc"
s2: 8 "abc^@^@def"
UKMonkey
  • 6,941
  • 3
  • 21
  • 30
13

Therefore, is the use of the "s" suffix only meant for the reader of the code?

No, its not only for the reader of the code, but tells the compiler which exact type to create from the literal.

Or are there others advantages to its use?

Sure: There is the advantage to write that code shorter but yet yielding the wanted type

auto hello = "Hello!"s;

As you noticed this creates a std::string and is the same as writing

auto hello = std::string{"Hello!"};

, whereas

auto hello = "Hello!";

would create a const char* pointer pointing to a const char[7] array.

TonySalimi
  • 8,257
  • 4
  • 33
  • 62
  • 5
    the question is 'whats these difference between the 2 std::string initalizations?' - not 'what happens if i leave the s off?' – pm100 Jan 26 '18 at 16:11
  • 1
    @pm100 Clearer now? –  Jan 26 '18 at 16:14
  • nope. Its obvious its less typing. But are there other advantages. For example is it more efficent at run time. std::string has/had no way of making constants - see https://stackoverflow.com/questions/2312860/correct-idiom-for-stdstring-constants – pm100 Jan 26 '18 at 16:17
  • @TheDude `auto hello = "Hello!";` would rather create a `const char *` than `const char[7]`; – Jabberwocky Jan 26 '18 at 16:17
  • ukmonkey points out a great advantage – pm100 Jan 26 '18 at 16:18
  • @pm100 Yes, the `\0` characters inline in that literal are a neat thing. –  Jan 26 '18 at 16:19
  • 1
    Although this actually answers the question "Is there a disadvantage to using the s suffix?", it still makes a really good point. It's easy to forget the s and wind up with the wrong type. – Ferruccio Jan 26 '18 at 16:23