Topic: static const member and auto
Author: Zhihao Yuan <zy@miator.net>
Date: Thu, 29 Aug 2013 01:57:51 -0400
Raw View
When a static const data member is odr-used, a definition
at the namespace scope is required. However, if
the member is defined in terms of auto...
struct X {
static constexpr auto a = 42;
};
constexpr int X::a; // no, you can't say constexpr auto X::a;
int main() {
cout << &X::a << endl;
}
This problem is annoying.
--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://4bsd.biz/
--
---
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/.
.
Author: Richard Smith <richard@metafoo.co.uk>
Date: Thu, 29 Aug 2013 14:29:13 -0700
Raw View
--047d7bf0ddf4271cf604e51cccf5
Content-Type: text/plain; charset=ISO-8859-1
On Wed, Aug 28, 2013 at 10:57 PM, Zhihao Yuan <zy@miator.net> wrote:
> When a static const data member is odr-used, a definition
> at the namespace scope is required. However, if
> the member is defined in terms of auto...
>
> struct X {
> static constexpr auto a = 42;
> };
>
> constexpr int X::a; // no, you can't say constexpr auto X::a;
>
You can say:
constexpr decltype(X::a) X::a;
.... but it's a little verbose. The corresponding case with a static data
member of a class template is worse:
template<typename T> struct X {
static constexpr auto a = T();
};
Here, I do not believe there is any way to provide a definition of the
static data member. Slightly unorthodox workaround:
template<typename T> struct X {
static constexpr auto &&a = T();
};
Now no definition is required, because it is not possible to odr-use a
reference that is initialized by a constant expression.
int main() {
> cout << &X::a << endl;
> }
>
> This problem is annoying.
>
> --
> Zhihao Yuan, ID lichray
> The best way to predict the future is to invent it.
> ___________________________________________________
> 4BSD -- http://4bsd.biz/
>
> --
>
> ---
> 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/.
>
--
---
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/.
--047d7bf0ddf4271cf604e51cccf5
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Wed, Aug 28, 2013 at 10:57 PM, Zhihao Yuan <span dir=3D=
"ltr"><<a href=3D"mailto:zy@miator.net" target=3D"_blank">zy@miator.net<=
/a>></span> wrote:<br><div class=3D"gmail_extra"><div class=3D"gmail_quo=
te"><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bor=
der-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:sol=
id;padding-left:1ex">
When a static const data member is odr-used, a definition<br>
at the namespace scope is required. =A0However, if<br>
the member is defined in terms of auto...<br>
<br>
struct X {<br>
=A0 =A0 static constexpr auto a =3D 42;<br>
};<br>
<br>
constexpr int X::a; =A0// no, you can't say constexpr auto X::a;<br></b=
lockquote><div><br></div><div>You can say:</div><div><br></div><div>=A0 =A0=
constexpr decltype(X::a) X::a;</div><div>=A0</div><div>... but it's a =
little verbose. The corresponding case with a static data member of a class=
template is worse:</div>
<div><br></div><div><div>template<typename T> struct X {</div><div>=
=A0 static constexpr auto a =3D T();</div><div>};</div></div><div><br></div=
><div>Here, I do not believe there is any way to provide a definition of th=
e static data member. Slightly unorthodox workaround:</div>
<div><br></div><div>template<typename T> struct X {</div><div>=A0 sta=
tic constexpr auto &&a =3D T();</div><div>};</div><div><br></div><d=
iv>Now no definition is required, because it is not possible to odr-use a r=
eference that is initialized by a constant expression.</div>
<div><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0p=
x 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-lef=
t-style:solid;padding-left:1ex">
int main() {<br>
=A0 =A0 cout << &X::a << endl;<br>
}<br>
<br>
This problem is annoying.<br>
<span class=3D""><font color=3D"#888888"><br>
--<br>
Zhihao Yuan, ID lichray<br>
The best way to predict the future is to invent it.<br>
___________________________________________________<br>
4BSD -- <a href=3D"http://4bsd.biz/" target=3D"_blank">http://4bsd.biz/</a>=
<br>
<br>
--<br>
<br>
---<br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org">std-propo=
sals+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/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</font></span></blockquote></div><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" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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 />
--047d7bf0ddf4271cf604e51cccf5--
.
Author: David Krauss <potswa@gmail.com>
Date: Fri, 30 Aug 2013 05:09:00 -0700 (PDT)
Raw View
------=_Part_513_24897751.1377864540161
Content-Type: text/plain; charset=ISO-8859-1
On Friday, August 30, 2013 5:29:13 AM UTC+8, Richard Smith wrote:
>
> template<typename T> struct X {
> static constexpr auto &&a = T();
> };
>
> Now no definition is required, because it is not possible to odr-use a
> reference that is initialized by a constant expression.
>
Does this actually produce well-defined behavior with multiple TUs?
Whether or not it does (but especially if it does), I'm baffled by the
requirement for an explicit definition. And it's strange that nonstatic
members of any type may be initialized at class scope but static members
seem to be stuck in a C++03 timewarp. Static member initialization needs a
revision.
--
---
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_513_24897751.1377864540161
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><br>On Friday, August 30, 2013 5:29:13 AM UTC+8, Richa=
rd Smith wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-=
left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr=
">template<typename T> struct X {<div><div class=3D"gmail_quote"><div=
> static constexpr auto &&a =3D T();</div><div>};</div><div><=
br></div><div>Now no definition is required, because it is not possible to =
odr-use a reference that is initialized by a constant expression.</div></di=
v></div></div></blockquote><div><br>Does this actually produce well-defined=
behavior with multiple TUs?<br><br>Whether or not it does (but especially =
if it does), I'm baffled by the requirement for an explicit definition. And=
it's strange that nonstatic members of any type may be initialized at clas=
s scope but static members seem to be stuck in a C++03 timewarp. Static mem=
ber initialization needs a revision.<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" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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_513_24897751.1377864540161--
.
Author: Richard Smith <richard@metafoo.co.uk>
Date: Fri, 30 Aug 2013 12:34:17 -0700
Raw View
--20cf307f37c2f84e2804e52f4eee
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Aug 30, 2013 at 5:09 AM, David Krauss <potswa@gmail.com> wrote:
>
>
> On Friday, August 30, 2013 5:29:13 AM UTC+8, Richard Smith wrote:
>>
>> template<typename T> struct X {
>> static constexpr auto &&a = T();
>> };
>>
>> Now no definition is required, because it is not possible to odr-use a
>> reference that is initialized by a constant expression.
>>
>
> Does this actually produce well-defined behavior with multiple TUs?
>
Yes. (See [basic.def.odr](3.2)/6 for the details: the program behaves "as
if there were a single definition of D".)
Whether or not it does (but especially if it does), I'm baffled by the
> requirement for an explicit definition.
>
This does work (in a sufficiently modern compiler!), but it requires that
the temporary is initialized by a constant expression. For some reason, the
need for dynamic initialization is tied to the need for an out-of-class
definition.
> And it's strange that nonstatic members of any type may be initialized at
> class scope but static members seem to be stuck in a C++03 timewarp. Static
> member initialization needs a revision.
>
Agreed, this is a perennial irritation, and especially for static data
members of class templates, is very hard to justify.
Strawman proposal: make a static data member of a class template be a
definition if either (1) it is declared within the class with an
initializer, or (2) it is declared outside the class and the in-class
declaration was not a definition, and allow any static data member to be
initialized within the class. We would also need some analysis to be sure
that existing implementations can support this in a
reasonably-backwards-compatible way.
--
---
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/.
--20cf307f37c2f84e2804e52f4eee
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Fri, Aug 30, 2013 at 5:09 AM, David Krauss <span dir=3D=
"ltr"><<a href=3D"mailto:potswa@gmail.com" target=3D"_blank">potswa@gmai=
l.com</a>></span> wrote:<br><div class=3D"gmail_extra"><div class=3D"gma=
il_quote">
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div class=3D"im"><br><br>O=
n Friday, August 30, 2013 5:29:13 AM UTC+8, Richard Smith wrote:<blockquote=
class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px =
#ccc solid;padding-left:1ex">
<div dir=3D"ltr">template<typename T> struct X {<div><div class=3D"gm=
ail_quote"><div>=A0 static constexpr auto &&a =3D T();</div><div>};=
</div><div><br></div><div>Now no definition is required, because it is not =
possible to odr-use a reference that is initialized by a constant expressio=
n.</div>
</div></div></div></blockquote></div><div><br>Does this actually produce we=
ll-defined behavior with multiple TUs?<br></div></div></blockquote><div><br=
></div><div>Yes. (See [basic.def.odr](3.2)/6 for the details: the program b=
ehaves "as if there were a single definition of D".)</div>
<div><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex=
;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>Whether=
or not it does (but especially if it does), I'm baffled by the require=
ment for an explicit definition.</div>
</div></blockquote><div><br></div><div>This does work (in a sufficiently mo=
dern compiler!), but it requires that the temporary is initialized by a con=
stant expression. For some reason, the need for dynamic initialization is t=
ied to the need for an out-of-class definition.</div>
<div>=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;=
border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>And it&#=
39;s strange that nonstatic members of any type may be initialized at class=
scope but static members seem to be stuck in a C++03 timewarp. Static memb=
er initialization needs a revision.<br>
</div></div></blockquote><div><br></div><div>Agreed, this is a perennial ir=
ritation, and especially for static data members of class templates, is ver=
y hard to justify.</div><div><br></div><div>Strawman proposal: make a stati=
c data member of a class template be a definition if either (1) it is decla=
red within the class with an initializer, or (2) it is declared outside the=
class and the in-class declaration was not a definition, and allow any sta=
tic data member to be initialized within the class. We would also need some=
analysis to be sure that existing implementations can support this in a re=
asonably-backwards-compatible way.</div>
</div></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" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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 />
--20cf307f37c2f84e2804e52f4eee--
.