Boost C++ Libraries: Ticket #12980: compile time character array https://svn.boost.org/trac10/ticket/12980 <p> The string_view has the following constructor: </p> <div class="wiki-code"><div class="code"><pre><span class="n">BOOST_CONSTEXPR</span> <span class="nf">basic_string_view</span><span class="p">(</span><span class="k">const</span> <span class="n">charT</span><span class="o">*</span> <span class="n">str</span><span class="p">)</span> <span class="o">:</span> <span class="n">ptr_</span><span class="p">(</span><span class="n">str</span><span class="p">),</span> <span class="n">len_</span><span class="p">(</span><span class="n">traits</span><span class="o">::</span><span class="n">length</span><span class="p">(</span><span class="n">str</span><span class="p">))</span> <span class="p">{}</span> </pre></div></div><p> This uses a 'length' call to determine its length. It would be nice if it could differentiate between compile time array's and other strings. However others have failed as well: </p> <p> <a class="ext-link" href="http://stackoverflow.com/questions/36459703/compile-time-strings-constructor-overload-precedence-between-const-char"><span class="icon">​</span>http://stackoverflow.com/questions/36459703/compile-time-strings-constructor-overload-precedence-between-const-char</a> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12980 Trac 1.4.3 Marshall Clow Thu, 20 Apr 2017 15:18:48 GMT owner changed https://svn.boost.org/trac10/ticket/12980#comment:1 https://svn.boost.org/trac10/ticket/12980#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">No-Maintainer</span> to <span class="trac-author">Marshall Clow</span> </li> </ul> <p> As you note, we've been down this rathole before. You can pass pointer and length; there's a constructor for that. </p> <p> And then there's the whole "trailing null" issue: </p> <pre class="wiki">const char arr1 [] = { 'H', 'e', 'l', 'l', 'o' }; const char arr2 [] = "Hello"; const *char p1 = "Hello"; </pre><p> If you make a <code>string_view</code> from <code>arr1</code>, its <code>size()</code> should be five, because there are five characters in the array. </p> <p> If you make a <code>string_view</code> from <code>p1</code>, its <code>size()</code> should be five, because that's what <code>traits::length</code> returns. </p> <p> What about <code>arr2</code>? Should it be 6, since there are six characters in the array? Or 5, since the last character is a NUL? </p> Ticket gast128@… Thu, 20 Apr 2017 16:54:38 GMT <link>https://svn.boost.org/trac10/ticket/12980#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12980#comment:2</guid> <description> <p> The trailing zero is indeed a conceptual problem of how to deal with character arrays vs string literals. Alternatively could it be solved by introducing yet another class / macro and overload for that, e.g. </p> <div class="wiki-code"><div class="code"><pre><span class="n">BOOST_DEFINE_STRING_CONST</span><span class="p">(</span><span class="n">_T</span><span class="p">(</span><span class="s">&quot;Hello&quot;</span><span class="p">));</span> <span class="c1">//introduces a class convertible to string_view</span> </pre></div></div><p> In our code base we largely use (w)strings. A reason to prefer the string_view in arguments is to circumvent the superfluous conversion to a string object when hard code strings are supplied in the arguments. </p> </description> <category>Ticket</category> </item> <item> <author>gast128@…</author> <pubDate>Sun, 23 Apr 2017 18:07:09 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12980#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12980#comment:3</guid> <description> <p> Or </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#define BOOST_DEFINE_STRINGA_VIEW(str) \</span> <span class="cp"> string_view((str), _countof((str)) - 1)</span> </pre></div></div><p> Drawbacks that it is not much of an improvement and that Stroustrup doesn't like macro's. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Fri, 05 Jan 2018 03:26:24 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/12980#comment:4 https://svn.boost.org/trac10/ticket/12980#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <blockquote class="citation"> <p> Drawbacks that it is not much of an improvement. </p> </blockquote> <p> Yes, I think this is not really an improvement. </p> <p> In C++17, we got a constexpr char_traits::length, specifically to solve this problem. People can do that with <code>boost::string_view</code> as well (define their own <code>traits</code> class, with a constexpr <code>length</code> call). </p> <p> Closing this as "wont fix" (even though it's not a bug, but a feature request) </p> Ticket