struct U
{
auto ref func(int i)() { return this; }
}
void main()
{
{
alias type = typeof(U().func!0);
pragma(msg, type); // pure nothrow @nogc ref @safe U() return
//.1
pragma(msg, is(type : U)); // 假
auto u = U().func!0;
pragma(msg, typeof(u)); // U
}
{
alias type = typeof(U().func!0());
pragma(msg, type); // U
pragma(msg, is(type : U)); // 真
auto u = U().func!0();
pragma(msg, typeof(u)); // U
}
}
为何,typeof(U().func!0)!=U
.
如何检查
返回的值是否具有U类型
.
.1
就是@property
不同的地方,
@property auto ref func(int i)() { return this; }
现在,U().func!0
为式中调用
,但不推荐@property
.
std.traits.ReturnType
更明白.
import std.traits;
alias type = ReturnType!(U().func!0);
为何不直接:
alias type = typeof(U().func!0());
本文同步分享在 博客“fqbqrr”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。