www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - pass by value && elide dtor + post-blit

reply "Xiaoxi" <xiaoxi 163.com> writes:
When passing a struct by value:

Is there any way to trick the compiler to elide unnecessary 
post-blit & dtor pair?

Maybe using an union, somehow?
Jun 20 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/20/2015 02:09 PM, Xiaoxi wrote:
 When passing a struct by value:

 Is there any way to trick the compiler to elide unnecessary post-blit &
 dtor pair?

 Maybe using an union, somehow?
Can you show with an example please. I don't see either of those called for the following program: import std.stdio; struct S { this(int) { writeln(__FUNCTION__); } this(this) { writeln(__FUNCTION__); } ~this() { writeln(__FUNCTION__); } } S foo() { auto s = S(42); return s; } void main() { writeln("before"); auto s = foo(); writeln("after"); } The output: before deneme.S.this after deneme.S.~this Ali
Jun 20 2015
parent reply "Xiaoxi" <xiaoxi 163.com> writes:
On Saturday, 20 June 2015 at 22:44:17 UTC, Ali Çehreli wrote:
 On 06/20/2015 02:09 PM, Xiaoxi wrote:
 The output:

 before
 deneme.S.this
 after
 deneme.S.~this

 Ali
Dear Ali, thank you for helping! Problem happens when passing by value as in param. DMD32 D Compiler v2.067.0 deneme.S.this before deneme.S.__postblit deneme.S.__postblit deneme.S.~this deneme.S.~this after deneme.S.~this import std.stdio; struct S { int val; this(int par) { val = par; writeln(__FUNCTION__); } this(this) { writeln(__FUNCTION__); } ~this() { writeln(__FUNCTION__); } } S foo(S s) { s.val+=1; return s; } void main() { auto s = S(42); writeln("before"); foo(s); writeln("after"); }
Jun 21 2015
parent "Namespace" <rswhite4 gmail.com> writes:
 Dear Ali,

 thank you for helping! Problem happens when passing by value as 
 in param.
Change 'foo' to this: ---- ref S foo(ref S s) { s.val+=1; return s; } ----
Jun 21 2015