Topic: constexpr default constructors for containers where possible


Author: Matthew Fioravante <fmatthew5876@gmail.com>
Date: Fri, 27 Feb 2015 18:50:32 -0800 (PST)
Raw View
------=_Part_1_1725379795.1425091832762
Content-Type: multipart/alternative;
 boundary="----=_Part_2_986529124.1425091832762"

------=_Part_2_986529124.1425091832762
Content-Type: text/plain; charset=UTF-8

Having a way to constexpr construct data structures is very useful when you
want to make a global data structure because it eliminates the static
initialization order race condition.

class WidgetTable {
  public:
    static Widget& lookup(std::string name);
    static Widget& lookup(int id) { return _widgets[id]; }
    static int getId(const std::string& name);

  private:
    static std::unordered_map<std::string, int> _name_to_id;
    static std::vector<Widget> _widgets;
};

At a minimum if at least vector, unordered_map, and string have constexpr
default constructors if its possible would be enough for a lot of use cases.

Assuming the underlying allocator constructor is constexpr, vector seems
pretty straightforward. No sane vector implementation should be
speculatively allocating memory in the default constructor anyway.

string should be simple as well, especially with the SSO.

unordered_map I'm not so sure of.

Today the only portable way to get this behavior on your own is to
reimplement all of these data structures yourself.

Even if modules solve the static ordering problem, global objects still
have runtime overhead before main() is called. Some projects like LLVM have
strict policies about limiting these things for performance.

Are there any serious problems with trying to make the default constructors
constexpr for these and other data structures?

--

---
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_2_986529124.1425091832762
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Having a way to constexpr construct data structures is ver=
y useful when you want to make a global data structure because it eliminate=
s the static initialization order race condition.<div><br></div><div><div c=
lass=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wr=
ap: break-word; background-color: rgb(250, 250, 250);"><code class=3D"prett=
yprint"><div class=3D"subprettyprint"><span style=3D"color: #008;" class=3D=
"styled-by-prettify">class</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-pre=
ttify">WidgetTable</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </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">public</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; &nbsp; </sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">static</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span styl=
e=3D"color: #606;" class=3D"styled-by-prettify">Widget</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">&amp;</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> lookup</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">std</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">::</span><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">string</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> name</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&=
nbsp; &nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">static</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Widget</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> lookup</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D=
"color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color:=
 #000;" class=3D"styled-by-prettify"> id</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-b=
y-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">return=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><f=
ont color=3D"#666600"><span style=3D"color: #000;" class=3D"styled-by-prett=
ify">_widgets</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">[</span><span style=3D"color: #000;" class=3D"styled-by-prettify">id</sp=
an><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></span></font><span style=3D"color: =
#000;" class=3D"styled-by-prettify">&nbsp; &nbsp; </span><span style=3D"col=
or: #008;" class=3D"styled-by-prettify">static</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> getId</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-pre=
ttify">const</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">string</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> name</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">)</span><font color=3D"#00=
0000"><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span></f=
ont><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br>&nbsp=
; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">private<=
/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; </=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">static</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">unordered_map</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">std</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">string</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-pre=
ttify">int</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> _name=
_to_id</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &n=
bsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">stati=
c</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> std</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">vector</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"c=
olor: #606;" class=3D"styled-by-prettify">Widget</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> _widgets</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"st=
yled-by-prettify">};</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br></span></div></code></div><br>At a minimum if at least vector=
, unordered_map, and string have constexpr default constructors if its poss=
ible would be enough for a lot of use cases.</div><div><br></div><div>Assum=
ing the underlying allocator constructor is constexpr, vector seems pretty =
straightforward. No sane vector implementation should be speculatively allo=
cating memory in the default constructor anyway.</div><div><br></div><div>s=
tring should be simple as well, especially with the SSO.</div><div><br></di=
v><div>unordered_map I'm not so sure of.</div><div><br></div><div>Today the=
 only portable way to get this behavior on your own is to reimplement all o=
f these data structures yourself.</div><div><br></div><div>Even if modules =
solve the static ordering problem, global objects still have runtime overhe=
ad before main() is called. Some projects like LLVM have strict policies ab=
out limiting these things for performance.</div><div><br></div><div>Are the=
re any serious problems with trying to make the default constructors conste=
xpr for these and other data structures?</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_2_986529124.1425091832762--
------=_Part_1_1725379795.1425091832762--

.