This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/36744] function modifying argument received by-value affects caller's variable when passed as rvalue
- From: "chris dot fairles at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 6 Jul 2008 19:33:46 -0000
- Subject: [Bug c++/36744] function modifying argument received by-value affects caller's variable when passed as rvalue
- References: <bug-36744-5473@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Comment #2 from chris dot fairles at gmail dot com 2008-07-06 19:33 -------
(In reply to comment #0)
> Consider:
>
> #include <iostream>
>
> struct S
> {
> S(): i(2) {}
//S(S const&) {}
S(S const&);
The cctor need only be defined as well to get the same behavior (gcc 4.4.0,
-std=c++0x, -fno-elide-constructors, -O0, -fno-inline).
> int i;
> };
>
> void f(S x) { x.i = 0; }
The argument type of f(S), so far as my very limited understanding of gimple
goes, is the class type S when the cctor is defined (also marks it for copy
construction). Without the def'n, it's transformed to a reference type (struct
S & restrict) which I assume is an rvalue ref and x just gets moved. Been
trying to locate the responsible code w/o luck.
Chris
>
> int main()
> {
> S y;
> f(static_cast<S&&>(y));
> std::cout << y.i << '\n';
> }
>
> Expected output: 2
> Actual output: 0
>
> Thus, the assignment to the independent local variable x in f somehow modifies
> y. That can't be right, not even with that static_cast to S&&, can it?
>
--
chris dot fairles at gmail dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |chris dot fairles at gmail
| |dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36744