Topic: The sorry state of not odr-using a class type


Author: Zhihao Yuan <zy@miator.net>
Date: Thu, 12 Sep 2013 16:00:18 -0400
Raw View
Lvalue-to-rvalue conversion allows class type's copy
initialization, un-odr-use allows lvalue-to-rvalue conversion,
but copy/move constructor needs to take reference.

The following example fails with or without comment
those constructors out.

#include <utility>

struct A {
    //constexpr A() : n_()
    //{}
    //constexpr A(A const& a) : n_(a.n_)
    //{}

    int n_;
};

struct B {
    static constexpr auto a = A();
};

void f(A a) {
}

int main() {
    static_assert(std::is_literal_type<A>::value, "orz");
    f(B::a);
}

I think it make sense to allow a literal type for not
being odr-used in such a case, though.  Correct me
if I'm wrong.

--
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, 12 Sep 2013 13:12:56 -0700
Raw View
--047d7b6d8cc81ff52d04e6355d9d
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Sep 12, 2013 at 1:00 PM, Zhihao Yuan <zy@miator.net> wrote:

> Lvalue-to-rvalue conversion allows class type's copy
> initialization, un-odr-use allows lvalue-to-rvalue conversion,
> but copy/move constructor needs to take reference.
>
> The following example fails with or without comment
> those constructors out.
>
> #include <utility>
>
> struct A {
>     //constexpr A() : n_()
>     //{}
>     //constexpr A(A const& a) : n_(a.n_)
>     //{}
>
>     int n_;
> };
>
> struct B {
>     static constexpr auto a = A();
> };
>
> void f(A a) {
> }
>
> int main() {
>     static_assert(std::is_literal_type<A>::value, "orz");
>     f(B::a);
>

This does not perform an lvalue-to-rvalue conversion on B::a (instead, it
implicitly calls the copy constructor via the copy-initialization rules).
The problem you describe exists for a case like:

void f(...) {}

This is core issue 1741; B::a should be odr-used in such a case:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1741

}
>
> I think it make sense to allow a literal type for not
> being odr-used in such a case, though.  Correct me
> if I'm wrong.
>
> --
> 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/.

--047d7b6d8cc81ff52d04e6355d9d
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Thu, Sep 12, 2013 at 1:00 PM, Zhihao Yuan <span dir=3D"=
ltr">&lt;<a href=3D"mailto:zy@miator.net" target=3D"_blank">zy@miator.net</=
a>&gt;</span> wrote:<br><div class=3D"gmail_extra"><div class=3D"gmail_quot=
e"><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bord=
er-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:soli=
d;padding-left:1ex">
Lvalue-to-rvalue conversion allows class type&#39;s copy<br>
initialization, un-odr-use allows lvalue-to-rvalue conversion,<br>
but copy/move constructor needs to take reference.<br>
<br>
The following example fails with or without comment<br>
those constructors out.<br>
<br>
#include &lt;utility&gt;<br>
<br>
struct A {<br>
=A0 =A0 //constexpr A() : n_()<br>
=A0 =A0 //{}<br>
=A0 =A0 //constexpr A(A const&amp; a) : n_(a.n_)<br>
=A0 =A0 //{}<br>
<br>
=A0 =A0 int n_;<br>
};<br>
<br>
struct B {<br>
=A0 =A0 static constexpr auto a =3D A();<br>
};<br>
<br>
void f(A a) {<br>
}<br>
<br>
int main() {<br>
=A0 =A0 static_assert(std::is_literal_type&lt;A&gt;::value, &quot;orz&quot;=
);<br>
=A0 =A0 f(B::a);<br></blockquote><div><br></div><div>This does not perform =
an lvalue-to-rvalue conversion on B::a (instead, it implicitly calls the co=
py constructor via the copy-initialization rules). The problem you describe=
 exists for a case like:</div>
<div><br></div><div>void f(...) {}</div><div><br></div><div>This is core is=
sue 1741; B::a should be odr-used in such a case:</div><div><br></div><div>=
<a href=3D"http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1741=
">http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1741</a><br>
</div><div><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px =
0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);bord=
er-left-style:solid;padding-left:1ex">
}<br>
<br>
I think it make sense to allow a literal type for not<br>
being odr-used in such a case, though. =A0Correct me<br>
if I&#39;m wrong.<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&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%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 />
&nbsp;<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 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 />

--047d7b6d8cc81ff52d04e6355d9d--

.


Author: Zhihao Yuan <zy@miator.net>
Date: Thu, 12 Sep 2013 16:46:17 -0400
Raw View
On Thu, Sep 12, 2013 at 4:12 PM, Richard Smith <richard@metafoo.co.uk> wrote:
> This does not perform an lvalue-to-rvalue conversion on B::a (instead, it
> implicitly calls the copy constructor via the copy-initialization rules).

Function parameter initialization (5.2.2/4) is not defined in
terms of l-r conversion but operators do?  Interesting...

> This is core issue 1741; B::a should be odr-used in such a case:
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1741

Got it.  Thanks.  Just, can't think of a solution :(

--
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: Zhihao Yuan <zy@miator.net>
Date: Thu, 12 Sep 2013 22:53:48 -0400
Raw View
On Thu, Sep 12, 2013 at 4:12 PM, Richard Smith <richard@metafoo.co.uk> wrote:
> This does not perform an lvalue-to-rvalue conversion on B::a (instead, it
> implicitly calls the copy constructor via the copy-initialization rules).
> The problem you describe exists for a case like:
>
> void f(...) {}

By unifying the requirements of function argument initialization
and lvalue-to-rvalue conversion (4.1/2), I think the key problem
can be unified to "copy initialization of a class type can not
be done in a non odr-used way".

--
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: David Krauss <potswa@gmail.com>
Date: Thu, 12 Sep 2013 21:18:01 -0700 (PDT)
Raw View
------=_Part_1076_18065393.1379045881703
Content-Type: text/plain; charset=ISO-8859-1



On Friday, September 13, 2013 10:53:48 AM UTC+8, Zhihao Yuan wrote:
>
> By unifying the requirements of function argument initialization
> and lvalue-to-rvalue conversion (4.1/2), I think the key problem
> can be unified to "copy initialization of a class type can not
> be done in a non odr-used way".
>

It sounds like you want ODR use of an object of literal class type to have
a transitive relationship from ODR use of its members. Can you elaborate on
the motivating use case? Or you just don't want to write the out-of-class
definition of B::a?

The workaround, I suppose, would be to use aggregate initialization instead
of copy construction. This fixes it for GCC and Clang:

    f({B::a.n_});

To be sure, the *class* is ODR used if you have any object of it or refer
to any member, even in an unevaluated context. The title of this thread was
initially very confusing to me. You are concerned with the definition of
the object, not the type.

--

---
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_1076_18065393.1379045881703
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Friday, September 13, 2013 10:53:48 AM UTC+8, Z=
hihao Yuan wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margi=
n-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">By unifying t=
he requirements of function argument initialization
<br>and lvalue-to-rvalue conversion (4.1/2), I think the key problem
<br>can be unified to "copy initialization of a class type can not
<br>be done in a non odr-used way".
<br></blockquote><div><br>It sounds like you want ODR use of an object of l=
iteral class type to have a transitive relationship from ODR use of its mem=
bers. Can you elaborate on the motivating use case? Or you just don't want =
to write the out-of-class definition of <span style=3D"font-family: courier=
 new,monospace;">B::a</span>?<br><br>The workaround, I suppose, would be to=
 use aggregate initialization instead of copy construction. This fixes it f=
or GCC and Clang:<br><br><div class=3D"prettyprint" style=3D"background-col=
or: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style: sol=
id; border-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint">=
<div class=3D"subprettyprint"><span style=3D"color: #000;" class=3D"styled-=
by-prettify">&nbsp; &nbsp; f</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">({</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify">B</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>::</span><span style=3D"color: #000;" class=3D"styled-by-prettify">a</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">n_</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">});</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"><br></span></div></code></div><br>To be =
sure, the <i>class</i> is ODR used if you have any object of it or refer to=
 any member, even in an unevaluated context. The title of this thread was i=
nitially very confusing to me. You are concerned with the definition of the=
 object, not the type.<br><br></div></div>

<p></p>

-- <br />
&nbsp;<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 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_1076_18065393.1379045881703--

.