July 02, 2016 Re: Passing structs to functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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*.
|
July 02, 2016 Re: Passing structs to functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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.
|
July 02, 2016 Re: Passing structs to functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 makes sense, but for small things it often has an overall negative effect on performance, but I see people saying they ALWAYS use struct& just out of habit.
yep. passing small structs is not really different from passing something like `(int x, int y)`. it is fast, and you spare yourself from one indirection, which is inevitable with `ref`.
|
July 02, 2016 Re: Passing structs to functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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 template bloat.
not "dangerous", but *dangerous*.
|
July 02, 2016 Re: Passing structs to functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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 *dangerous*.
>>
>> Either that "dangerous" thing or 2^N template bloat.
>
> not "dangerous", but *dangerous*.
I see no real danger in that code snippet of mine. 'auto ref' is the wrong solution since it leads to 2^N template bloat and passing by value is only a good solution if your struct is really small.
|
July 02, 2016 Re: Passing structs to functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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) { writeln(a.id); } void main() { foo(A(42).byRef()); } ---- |
July 03, 2016 Re: Passing structs to functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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.
|
Copyright © 1999-2021 by the D Language Foundation