d的typeof问题2
原创
©著作权归作者所有:来自51CTO博客作者fqbqrr的原创作品,请联系作者获取转载授权,否则将追究法律责任
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
不会.因此,要处理这个用例,似乎需要实现自己的基于ReturnType
的hasUDA
和getUDAs
.
问题是,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
}