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
不会.因此,要处理这个用例,似乎需要实现自己的基于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
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现