Function template declaration mystery...
Steven Schveighoffer
schveiguy at yahoo.com
Wed Feb 28 18:25:37 UTC 2018
On 2/28/18 12:47 PM, Robert M. Münch wrote:
> Hi, I'm lost reading some code:
>
> A a;
>
> auto do(alias f, A)(auto ref A _a){
> alias fun = unaryFun!f;
> return ...
> ...
> }
>
> How is this alias stuff working? I mean what's the type of f? Is it an
> anonymous function which then gets checked to be unary? How is it
> recognized in the code using the function template?
unaryFun is a template that returns a callable item. It could be a
struct with an opCall, it could be a function template, it could be an
alias to a real function, it could be a function pointer, delegate, etc.
It also supports string lambdas, which existed before our current lambda
syntax.
i.e. alias f = unaryFun!"a - 5";
assert(f(10) == 5);
>
>
> This function can be called with code like this:
>
> a.do((myType) {...myCode...});
> do(a, (myType) {...myCode...});
>
> What's wondering me here is that the template function only has one
> paraemter (_a) but I somehow can get my myCode into it. But the code
> looks like a parameter to me. So why isn't it like:
>
> auto do(alias f, A)(auto ref A _a, ??? myCode){...
>
> I'm a bit confused.
This question is a little harder to understand. Perhaps you have real
code that shows what you are confused about?
-Steve
More information about the Digitalmars-d-learn
mailing list