Re: Passing structs to functions

2016-07-02 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 2 July 2016 at 21:23:57 UTC, Namespace wrote: passing by value is only a good solution if your struct is really small. It's not uncommon for optimizers to generate the same code either way regardless of what you write.

Re: Passing structs to functions

2016-07-02 Thread Namespace via Digitalmars-d-learn
Just for you, a slightly adapted version: import std.stdio; struct A { public int id = 0; this(int id) { this.id = id; } ref A byRef() { return this; } } void foo(ref const A a) {

Re: Passing structs to functions

2016-07-02 Thread Namespace via Digitalmars-d-learn
On Saturday, 2 July 2016 at 21:19:04 UTC, ketmar wrote: On Saturday, 2 July 2016 at 21:17:33 UTC, Namespace wrote: On Saturday, 2 July 2016 at 21:15:29 UTC, ketmar wrote: On Saturday, 2 July 2016 at 21:05:18 UTC, Namespace wrote: Try this little trick: or don't. such pointers to structs are

Re: Passing structs to functions

2016-07-02 Thread ketmar via Digitalmars-d-learn
On Saturday, 2 July 2016 at 21:17:33 UTC, Namespace wrote: On Saturday, 2 July 2016 at 21:15:29 UTC, ketmar wrote: On Saturday, 2 July 2016 at 21:05:18 UTC, Namespace wrote: Try this little trick: or don't. such pointers to structs are *dangerous*. Either that "dangerous" thing or 2^N

Re: Passing structs to functions

2016-07-02 Thread ketmar via Digitalmars-d-learn
On Saturday, 2 July 2016 at 20:40:00 UTC, Adam D. Ruppe wrote: Using ref is wasteful there regardless just take an ordinary Point (even const is optional if it is all value but it doesn't hurt). I think a lot of C++ programmers overuse references. If you're passing a large thing, it

Re: Passing structs to functions

2016-07-02 Thread Namespace via Digitalmars-d-learn
On Saturday, 2 July 2016 at 21:15:29 UTC, ketmar wrote: On Saturday, 2 July 2016 at 21:05:18 UTC, Namespace wrote: Try this little trick: or don't. such pointers to structs are *dangerous*. Either that "dangerous" thing or 2^N template bloat.

Re: Passing structs to functions

2016-07-02 Thread ketmar via Digitalmars-d-learn
On Saturday, 2 July 2016 at 21:05:18 UTC, Namespace wrote: Try this little trick: or don't. such pointers to structs are *dangerous*.

Re: Passing structs to functions

2016-07-02 Thread Namespace via Digitalmars-d-learn
On Saturday, 2 July 2016 at 19:40:53 UTC, phant0m wrote: On Saturday, 2 July 2016 at 19:25:37 UTC, ketmar wrote: note the first "()", though: this is effectively a template function, which compiler will instantiate either with "ref" or without it. Yeah, I've noticed it. Always using function

Re: Passing structs to functions

2016-07-02 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 2 July 2016 at 20:24:12 UTC, phant0m wrote: I took a simple task (for D learning purposes): to implement a Point template "class" (something like Qt's QPoint). Using ref is wasteful there regardless just take an ordinary Point (even const is optional if it is all value but

Re: Passing structs to functions

2016-07-02 Thread phant0m via Digitalmars-d-learn
On Saturday, 2 July 2016 at 19:46:53 UTC, Adam D. Ruppe wrote: On Saturday, 2 July 2016 at 18:37:06 UTC, phant0m wrote: How should I pass a struct variable in D effectively? Passing by value is often the most efficient. It depends on what exactly you have in the struct. From the point of

Re: Passing structs to functions

2016-07-02 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 2 July 2016 at 18:37:06 UTC, phant0m wrote: How should I pass a struct variable in D effectively? Passing by value is often the most efficient. It depends on what exactly you have in the struct.

Re: Passing structs to functions

2016-07-02 Thread phant0m via Digitalmars-d-learn
On Saturday, 2 July 2016 at 19:25:37 UTC, ketmar wrote: note the first "()", though: this is effectively a template function, which compiler will instantiate either with "ref" or without it. Yeah, I've noticed it. Always using function template for this use case seems like a weird idea.

Re: Passing structs to functions

2016-07-02 Thread ketmar via Digitalmars-d-learn
On Saturday, 2 July 2016 at 18:47:31 UTC, phant0m wrote: On Saturday, 2 July 2016 at 18:43:51 UTC, ketmar wrote: void boo() (in auto ref MyStruct s) { ... } this will accept both lvalues and rvalues, and will avoid copying if it can. Thank you! Could you please explain what does "auto" in

Re: Passing structs to functions

2016-07-02 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Jul 02, 2016 at 06:47:31PM +, phant0m via Digitalmars-d-learn wrote: > On Saturday, 2 July 2016 at 18:43:51 UTC, ketmar wrote: > > void boo() (in auto ref MyStruct s) { ... } > > > > this will accept both lvalues and rvalues, and will avoid copying if > > it can. > > Thank you! Could

Re: Passing structs to functions

2016-07-02 Thread phant0m via Digitalmars-d-learn
On Saturday, 2 July 2016 at 18:43:51 UTC, ketmar wrote: void boo() (in auto ref MyStruct s) { ... } this will accept both lvalues and rvalues, and will avoid copying if it can. Thank you! Could you please explain what does "auto" in this context mean?

Re: Passing structs to functions

2016-07-02 Thread ketmar via Digitalmars-d-learn
void boo() (in auto ref MyStruct s) { ... } this will accept both lvalues and rvalues, and will avoid copying if it can.

Passing structs to functions

2016-07-02 Thread phant0m via Digitalmars-d-learn
I came from a C++ world, so I'm used to passing structs by a const reference (I mean the case, where a function argument isn't changed by the function). C++ allows passing a temporary (rvalue) to a function, which accepts a const reference. D doesn't allow this. All I have found is a message