Topic: Comments on string_view: creating a constexpr array


Author: Matthew Fioravante <fmatthew5876@gmail.com>
Date: Thu, 11 Dec 2014 17:17:58 -0800 (PST)
Raw View
------=_Part_113_1158285556.1418347078552
Content-Type: multipart/alternative;
 boundary="----=_Part_114_366079637.1418347078552"

------=_Part_114_366079637.1418347078552
Content-Type: text/plain; charset=UTF-8

It is a common idiom to create a global array of string literals at compile
time:

foo.h
class Foo {
  enum Color { kRed, kGreen, kBlue };
  static constexpr const char* const kColorStr[] = { "Red", "Green", "Blue"
};
};

foo.cpp
constexpr const char* const Foo::kColorStr[];

There are a few problems with this. One is that it uses a C array, which is
somewhat clumsy and error prone to use because it doesn't have a size()
member function (error prone sizeof() calculations to iterate using
indices) and it also decays to a pointer. The strings are also represented
by null terminated char* pointers which requires a strlen() call whenever
you need the length. The strlen() may be optimized out in somecases, but
strlen() isn't constexpr so you cannot get the string length at compile
time.

What would be ideal is to have the type of kColorStr be
std::array<string_view,N>. If we had a constexpr make_array() to construct
a std::array from a variable number of objects along with a string view
user defined literal (not in the proposal but mentioned as possibility in
the future) we could do this:

static constexpr auto kColorStr = make_array("Red"sv, "Green"sv, "Blue"sv);
//decltype(kColorStr) == std::array<string_view, 3>

Using an array<string_view,N> has other advantages, namely that it
automatically converts to an array_view<string_view>. This makes the string
table very easy to use in your program. Here is one example.

//Assumes E starts from 0 and is contiguous
template <typename E>
std::optional<E> enum_to_str(string_view s, carray_view<string_view> tags) {
  for(int i = 0; i < int(tags.size()); ++i) {
    if(s == tags[i]) {
      return E(i);
    }
  }
  return {};
}

assert(enum_to_str<Foo::Color>("Red", Foo::kColorStr) == Foo::Red);

A string_view[N] would also work here but const char* [N] or
std::array<const char*, N> would not. We want things to be string_view's or
strings and not char*. If the table is based on char*, you have to do a
conversion to string_view and construct temporary string_view tables if you
want to use string_views to interact with the table.

Using a string_view user defined literal "sv" has problems. The biggest
being that we have to add a using declaration to enable it. These constexpr
string tables will be defined in header files and adding using statements
to a header file is never a good idea.

I solved this problem by adding a simple static method to my string_view
class:

template <typename... Args>
constexpr std::array<string_view,sizeof...(Args)> string_view::
make_literal_array(Args... args) {
  return std::array<string_view,sizeof...(Args)>(string_view(args, sizeof(
args)-1)...);
}

This uses the (char*, size_t) constructor because there is no array
constructor (for good reason, see the paper), and the char* constructor
calls strlen() which is not constexpr. It also correctly supports string
literals with embedded nulls. This function will produce garbage if you
don't feed it string literals but we can probably add some static_asserts
to catch some of the incorrect usage. The name is obvious enough to not use
it the wrong way.

Example usage:

foo.h
class Foo {
  enum Color { kRed, kGreen, kBlue };
  static constexpr auto kColorStr = string_view::make_literal_array("Red",
"Green", "Blue");
};

foo.cpp
constexpr decltype(Foo::kColorStr) Foo::kColorStr;

Maybe something like my make_literal_array should be standardized? How
would you create a global constexpr array<string_view,N>?



--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_114_366079637.1418347078552
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">It is a common idiom to create a global array of string li=
terals at compile time:<div><br></div><div>foo.h</div><div><div class=3D"pr=
ettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-=
word; background-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><d=
iv class=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-by=
-prettify">class</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">Foo=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; </span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">enum</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #60=
6;" class=3D"styled-by-prettify">Color</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> kRed</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> kGr=
een</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> kBlue </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">};</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; </span><font col=
or=3D"#000088"><span style=3D"color: #008;" class=3D"styled-by-prettify">st=
atic</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #008;" class=3D"styled-by-prettify">constexpr</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">const</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">char</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">*</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">const</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> kColorStr</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">[]</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #0=
80;" class=3D"styled-by-prettify">"Red"</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by=
-prettify">"Green"</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #080;" class=3D"styled-by-prettify">"Blue"</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">};</span></font><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">};</span></div></code></div>=
<div><br></div>foo.cpp</div><div><div class=3D"prettyprint" style=3D"border=
: 1px solid rgb(187, 187, 187); word-wrap: break-word; background-color: rg=
b(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"subprettyprint=
"><font color=3D"#660066"><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">constexpr</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">c=
onst</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #008;" class=3D"styled-by-prettify">char</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">*</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=
=3D"styled-by-prettify">Foo</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">kColorStr</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">[];</span></font></div></code></div><br>There are a few problems wi=
th this. One is that it uses a C array, which is somewhat clumsy and error =
prone to use because it doesn't have a size() member function (error prone =
sizeof() calculations to iterate using indices) and it also decays to a poi=
nter. The strings are also represented by null terminated char* pointers wh=
ich requires a strlen() call whenever you need the length. The strlen() may=
 be optimized out in somecases, but strlen() isn't constexpr so you cannot =
get the string length at compile time.</div><div><br></div><div>What would =
be ideal is to have the type of kColorStr be std::array&lt;string_view,N&gt=
;. If we had a constexpr make_array() to construct a std::array from a vari=
able number of objects along with a string view user defined literal (not i=
n the proposal but mentioned as possibility in the future) we could do this=
:</div><div><span class=3D"styled-by-prettify" style=3D"font-family: monosp=
ace; font-size: 13.1428575515747px; color: rgb(0, 0, 0); background-color: =
rgb(250, 250, 250);"><br></span><font color=3D"#000088" style=3D"font-famil=
y: monospace; font-size: 13.1428575515747px; background-color: rgb(250, 250=
, 250);"><div class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187=
, 187); word-wrap: break-word; background-color: rgb(250, 250, 250);"><code=
 class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: =
#008;" class=3D"styled-by-prettify">static</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">constexpr</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">auto</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> kColorStr </span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> make_array</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">(</span><span style=3D"color: #080;" class=3D"styled-by-prettify">"Red"=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">sv</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #080;" class=3D"styled-by-prettify">"Green"</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify">sv</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-=
prettify">"Blue"</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify">sv</span><span style=3D"color: #660;" class=3D"styled-by-prettify">);=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span=
><span style=3D"color: #800;" class=3D"styled-by-prettify">//decltype(kColo=
rStr) =3D=3D std::array&lt;string_view, 3&gt;</span></div></code></div><spa=
n class=3D"styled-by-prettify" style=3D"color: rgb(102, 102, 0);"><br></spa=
n></font><span class=3D"styled-by-prettify" style=3D"font-family: monospace=
; font-size: 13.1428575515747px; color: rgb(0, 0, 0); background-color: rgb=
(250, 250, 250);">Using an array&lt;string_view,N&gt; has other advantages,=
 namely that it automatically converts to an array_view&lt;string_view&gt;.=
 This makes the string table very easy to use in your program. Here is one =
example.</span></div><div><span class=3D"styled-by-prettify" style=3D"font-=
family: monospace; font-size: 13.1428575515747px; color: rgb(0, 0, 0); back=
ground-color: rgb(250, 250, 250);"><br></span></div><div><span class=3D"sty=
led-by-prettify" style=3D"font-family: monospace; font-size: 13.14285755157=
47px; background-color: rgb(250, 250, 250);"><div class=3D"prettyprint" sty=
le=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; backgrou=
nd-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"su=
bprettyprint"><span style=3D"color: #800;" class=3D"styled-by-prettify">//A=
ssumes E starts from 0 and is contiguous</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">template</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">typename</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> E</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&=
gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>std=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify">optional</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify">E</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> enum_to_str</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">string_view s</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> carray_view</span><span style=3D"color: #080;" class=
=3D"styled-by-prettify">&lt;string_view&gt;</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> tags</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
><br>&nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify=
">for</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">int</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> i </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #066;=
" class=3D"styled-by-prettify">0</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> i </span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">int</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">tags</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">size</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">());</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">++</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">i</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">if</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">s </span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">=3D=3D</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> tags</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">[</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">i</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">])</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; &n=
bsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">retur=
n</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> E</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify">i</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"><br>&nbsp; </span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br>&nbsp; </span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">return</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">{};</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">}=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">assert</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">enum_to_str</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">Foo</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #606=
;" class=3D"styled-by-prettify">Color</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">&gt;(</span><span style=3D"color: #080;" class=
=3D"styled-by-prettify">"Red"</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify"=
>Foo</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</sp=
an><font color=3D"#660066"><span style=3D"color: #000;" class=3D"styled-by-=
prettify">kColorStr</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D=3D</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
 style=3D"color: #606;" class=3D"styled-by-prettify">Foo</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"colo=
r: #606;" class=3D"styled-by-prettify">Red</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">);</span></font></div></code></div><br>A st=
ring_view[N] would also work here but const char* [N] or std::array&lt;cons=
t char*, N&gt; would not. We want things to be string_view's or strings and=
 not char*. If the table is based on char*, you have to do a conversion to =
string_view and construct temporary string_view tables if you want to use s=
tring_views to interact with the table.</span></div><div><span class=3D"sty=
led-by-prettify" style=3D"font-family: monospace; font-size: 13.14285755157=
47px; background-color: rgb(250, 250, 250);"><br></span></div><div><span cl=
ass=3D"styled-by-prettify" style=3D"font-family: monospace; font-size: 13.1=
428575515747px; background-color: rgb(250, 250, 250);">Using a string_view =
user defined literal "sv" has problems. The biggest being that we have to a=
dd a using declaration to enable it. These constexpr string tables will be =
defined in header files and adding using statements to a header file is nev=
er a good idea.</span></div><div><span class=3D"styled-by-prettify" style=
=3D"font-family: monospace; font-size: 13.1428575515747px; background-color=
: rgb(250, 250, 250);"><br></span></div><div><span class=3D"styled-by-prett=
ify" style=3D"font-family: monospace; font-size: 13.1428575515747px; backgr=
ound-color: rgb(250, 250, 250);">I solved this problem by adding a simple s=
tatic method to my string_view class:</span></div><div><span class=3D"style=
d-by-prettify" style=3D"font-family: monospace; font-size: 13.1428575515747=
px; background-color: rgb(250, 250, 250);"><br></span></div><div><span clas=
s=3D"styled-by-prettify" style=3D"font-family: monospace; font-size: 13.142=
8575515747px; background-color: rgb(250, 250, 250);"><div class=3D"prettypr=
int" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; =
background-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div cla=
ss=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">template</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">typename</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">...</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">Args</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">constexpr</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> std</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">array</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify">string_view</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">,</span><span style=3D"color: #008;" class=3D"styled-by-prettify">siz=
eof</span><span style=3D"color: #660;" class=3D"styled-by-prettify">...(</s=
pan><span style=3D"color: #606;" class=3D"styled-by-prettify">Args</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">)&gt;</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> string_view</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">make_literal_array</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">Args</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">...</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> args</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br>&nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">return</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify">array</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify">string_view</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">sizeof</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">...(</span><span style=3D"color: #606;=
" class=3D"styled-by-prettify">Args</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">)&gt;(</span><font color=3D"#000000"><span style=
=3D"color: #000;" class=3D"styled-by-prettify">string_view</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">args</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">sizeof</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">args</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)-</=
span><span style=3D"color: #066;" class=3D"styled-by-prettify">1</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">)...);</span></font><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br></span></div></code></div><di=
v><span class=3D"styled-by-prettify" style=3D"font-family: monospace; font-=
size: 13.1428575515747px; background-color: rgb(250, 250, 250);"><br></span=
></div><div>This uses the (char*, size_t) constructor because there is no a=
rray constructor (for good reason, see the paper), and the char* constructo=
r calls strlen() which is not constexpr. It also correctly supports string =
literals with embedded nulls. This function will produce garbage if you don=
't feed it string literals but we can probably add some static_asserts to c=
atch some of the incorrect usage. The name is obvious enough to not use it =
the wrong way.</div><br>Example usage:</span></div><div><span class=3D"styl=
ed-by-prettify" style=3D"font-family: monospace; font-size: 13.142857551574=
7px; background-color: rgb(250, 250, 250);"><br></span></div><div><span cla=
ss=3D"styled-by-prettify" style=3D"font-family: monospace; font-size: 13.14=
28575515747px; background-color: rgb(250, 250, 250);">foo.h</span></div><di=
v><span class=3D"styled-by-prettify" style=3D"font-family: monospace; font-=
size: 13.1428575515747px; background-color: rgb(250, 250, 250);"><div class=
=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: =
break-word; background-color: rgb(250, 250, 250);"><code class=3D"prettypri=
nt"><div class=3D"subprettyprint"><span style=3D"color: #008;" class=3D"sty=
led-by-prettify">class</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prettif=
y">Foo</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; </span><spa=
n style=3D"color: #008;" class=3D"styled-by-prettify">enum</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #606;" class=3D"styled-by-prettify">Color</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">{</span><font color=3D"#000000"><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> kRed</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> kGreen</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> kBlue </span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">};</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br>&nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">static</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">constexp=
r</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> kColorStr </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> string_view</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">make_literal_array</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"col=
or: #080;" class=3D"styled-by-prettify">"Red"</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #080;" class=3D"sty=
led-by-prettify">"Green"</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> </span><span style=3D"color: #080;" class=3D"styled-by-prettify">"Blu=
e"</span><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">};</span></font></div>=
</code></div><div><span class=3D"styled-by-prettify" style=3D"font-family: =
monospace; font-size: 13.1428575515747px; background-color: rgb(250, 250, 2=
50);"><br></span></div>foo.cpp</span></div><div><span class=3D"styled-by-pr=
ettify" style=3D"font-family: monospace; font-size: 13.1428575515747px; bac=
kground-color: rgb(250, 250, 250);"><div class=3D"prettyprint" style=3D"bor=
der: 1px solid rgb(187, 187, 187); word-wrap: break-word; background-color:=
 rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"subprettypr=
int"><span style=3D"color: #008;" class=3D"styled-by-prettify">constexpr</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
 style=3D"color: #008;" class=3D"styled-by-prettify">decltype</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"c=
olor: #606;" class=3D"styled-by-prettify">Foo</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">kColorStr</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-pr=
ettify">Foo</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>::</span><span style=3D"color: #000;" class=3D"styled-by-prettify">kColorS=
tr</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span>=
</div></code></div><div><span class=3D"styled-by-prettify" style=3D"font-fa=
mily: monospace; font-size: 13.1428575515747px; background-color: rgb(250, =
250, 250);"><br></span></div>Maybe something like my make_literal_array sho=
uld be standardized? How would you create a global constexpr array&lt;strin=
g_view,N&gt;?<br><br></span><br><br></div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_114_366079637.1418347078552--
------=_Part_113_1158285556.1418347078552--

.