{"id":1739,"date":"2018-06-06T10:00:25","date_gmt":"2018-06-06T09:00:25","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/?p=1739"},"modified":"2018-06-07T12:49:06","modified_gmt":"2018-06-07T11:49:06","slug":"c17-stdstring_view","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2018\/06\/06\/c17-stdstring_view\/","title":{"rendered":"C++17: std::string_view"},"content":{"rendered":"<p>If you have to write a function that accepts a string, what type of parameter will you use? You have a couple of choices:<\/p>\n<ul>\n<li><strong>const std::string&amp;<\/strong>: downside is that whenever you call the function, an <strong>std::string<\/strong> is expected. If you pass a <strong>const char*<\/strong>, an <strong>std::string<\/strong> instance will be created implicitly for you.<\/li>\n<li><strong>const char*<\/strong>: downside is that if your caller has an <strong>std::string<\/strong>, they will have to use <strong>c_str()<\/strong> on it to pass a <strong>const char*<\/strong> to your function.<\/li>\n<li>A <strong>const char*<\/strong> overload and a <strong>const std::string&amp;<\/strong> overload: downside is that you have to implement two functions. This solution quickly becomes unwieldy if you are writing a method that accepts more than one string as parameter. For example, suppose your method requires two strings as parameters. If you want to overload the method to provide all possible combinations of input argument types, then you end up writing four versions: (<strong>const char*<\/strong>, <strong>const char*<\/strong>), (<strong>const char*<\/strong>, <strong>const string&amp;<\/strong>), (<strong>const string&amp;<\/strong>, <strong>const char*<\/strong>), and (<strong>const string&amp;<\/strong>, <strong>const string&amp;<\/strong>). This only becomes worse with even more string parameters.<\/li>\n<\/ul>\n<p>C++17 makes it easy by introducing a new type called <strong>std::string_view<\/strong>. From now on, if you are writing a function that accepts a string, use an <strong>std::string_view<\/strong> as parameter type. No need to use an <strong>std::string_view<\/strong> reference. A <strong>string_view<\/strong> is very cheap to copy, so it&#8217;s perfectly fine to pass by value. Basically, a <strong>string_view<\/strong> just contains a pointer to a string, and its length. A <strong>string_view<\/strong> parameter accepts any kind of string, such as a C++ <strong>std::string<\/strong>, a C-style <strong>const char*<\/strong> string, and a string literal, all without any copying involved!<\/p>\n<p><strong>std::string_view<\/strong> is defined in the <strong>&lt;string_view&gt;<\/strong> header. It has a similar interface as the well-known <strong>std::string<\/strong> class. However, since a <strong>string_view<\/strong> is a read-only view of a string, only the <strong>const<\/strong> operations of <strong>std::string<\/strong> are supported on a <strong>string_view<\/strong>. Additionally, a big advantage of a <strong>string_view<\/strong> is that it will never copy a string.<\/p>\n<p>Here is a very brief example of its usage:<\/p>\n<pre class=\"brush: cpp; highlight: [6,19,20,21]; title: ; notranslate\" title=\"\">#include &lt;iostream&gt;\r\n#include &lt;string_view&gt;\r\n\r\nusing namespace std;\r\n\r\nvoid ProcessString(string_view myString)\r\n{\r\n    cout &lt;&lt; myString; if (myString.size() &gt;= 4)\r\n    {\r\n        cout &lt;&lt; &quot;   (Substring: &quot; &lt;&lt; myString.substr(2, 2) &lt;&lt; &quot;)&quot;;\r\n    }\r\n    cout &lt;&lt; endl;\r\n}\r\n\r\nint main()\r\n{\r\n    string myString1 = &quot;Hello&quot;;\r\n    const char* myString2 = &quot;C++&quot;;\r\n    ProcessString(myString1);  \/\/ C++ string\r\n    ProcessString(myString2);  \/\/ C-style string\r\n    ProcessString(&quot;World!&quot;);   \/\/ String literal\r\n}<\/pre>\n<p>The output of this piece of code is as follows:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Hello   (Substring: ll)\r\nC++\r\nWorld!   (Substring: rl)<\/pre>\n<p>My book, <a href=\"https:\/\/amzn.to\/2JjvkWe\" target=\"_blank\" rel=\"noopener\">Professional C++, 4th Edition<\/a>, explains the <strong>std::string_view<\/strong> class in a bit more details. Additionally, it explains all new C++17 features, and much more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have to write a function that accepts a string, what type of parameter will you use? You have a couple of choices: const std::string&amp;: downside is that whenever you call the function, an std::string is expected. If you pass a const char*, an std::string instance will be created implicitly for you. const char*: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[222,228],"class_list":["post-1739","post","type-post","status-publish","format-standard","hentry","category-c","tag-c17","tag-visual-c-2017"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=1739"}],"version-history":[{"count":9,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1739\/revisions"}],"predecessor-version":[{"id":1753,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1739\/revisions\/1753"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=1739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=1739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=1739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}