templatized delegate
Stanislav Blinov via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue May 23 04:05:09 PDT 2017
On Tuesday, 23 May 2017 at 10:42:54 UTC, Nicholas Wilson wrote:
> On Tuesday, 23 May 2017 at 10:30:56 UTC, Alex wrote:
>> On Monday, 22 May 2017 at 21:44:17 UTC, ag0aep6g wrote:
>>> With that kind of variadics, you're not dealing with a
>>> template. A (run-time) variadic delegate is an actual
>>> delegate, i.e. a value that can be passed around. But the
>>> variadic stuff is a bit weird to use, and probably affects
>>> performance.
>>
>> By the way, I'm not even sure, if variadics work in my case. I
>> have a strange struct of a random generator, which cannot be
>> copied, and I have no idea how to pass it to a variadic
>> function:
>>
>> import std.stdio;
>> import mir.random;
>>
>> void main()
>> {
>> Random rndGen = Random(unpredictableSeed);
>> fun(rndGen);
>> }
>>
>> void fun(...)
>> {
>>
>> }
>>
>> Yields "... is not copyable because it is annotated with
>> @disable" :)
>
> Random is copy @disabled to prevent incorrect use.
> You need to pass it by ref or pointer. I dont know if you can
> pass variables as ref to a variadic, but you should be able to
> pass it by address.
> fun(&rndGen);
void variadic(Args...)(auto ref Args args) { /* ... */ }
This infers whether you pass lvalues or rvalues. If passing
further down the chain of such calls is needed, one can use
std.functional : fowrard :
void variadic(Args...)(auto ref Args args) {
import std.functional : forward;
doStuff(forward!args);
}
void doStuff(Args...)(auto ref Args args) {
/* ... */
}
'forward' aliases ref arguments (i.e. passed lvalues) and moves
value arguments (i.e. passed rvalues).
If a value is not copyable, it may be move-able (check the docs
though, it may not be that either).
void fun(Args...)(auto ref Args args) { /*...*/ }
import std.algorithm : move;
auto a = NonCopyable(42);
fun(move(a));
// or:
func(NonCopyable(42));
More information about the Digitalmars-d-learn
mailing list