d的typeof问题2

U().func!0不是调用吗?
这可行,但看起来很奇怪,我正在检查UDA表达式的类型:

@(U().func!0) int b;
pragma(msg,__traits(getAttributes,b));//tuple(U().func)
pragma(msg,typeof(__traits(getAttributes,b)[0]));//purenothrow@nogcref@safeU()return
pragma(msg,ReturnType!(__traits(getAttributes,b)[0]));//U
pragma(msg,is(typeof(__traits(getAttributes,b)[0]):U));//false
pragma(msg,hasUDA!(b,U));//false

包装在lambda中.

import std.traits;

alias type = ReturnType!(() => U().func!0);

或无需导入phobos:

alias type = typeof((() => U().func!0)());
//这样
auto ref eval(T)(auto ref T expr) { return expr; }
alias type = typeof(eval(U().func!0)));

我的情况是,用户可编写UDA表达式,我检查它是否是使用hasUDA!(sym,U)getUDAs!(sym,U)U类型.用户使用U()U().func!0(),则一切正常.但因为类型不是U,U().func!0不会.因此,要处理这个用例,似乎需要实现自己的基于ReturnTypehasUDAgetUDAs.
问题是,typeof(U().func!0)为何与typeof(U().func!0())的类型不一样.
UFCS中也存在不一致,类型根据函数是成员还是自由(即使用ref U替换auto ref)

struct U
{
    ref U func(int i)() { return this; }
}
ref U func2(int i)(ref U u) { return u; }

void main()
{
    U u;
    pragma(msg, typeof(u.func!0));//pure nothrow @nogc ref @safe U() return
    pragma(msg, typeof(u.func2!0));//U
}
posted @ 2022-08-22 23:29  zjh6  阅读(17)  评论(0)    收藏  举报  来源