d的typeof问题

原创
2022/08/22 22:45
阅读数 52

原文

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源创计划”,欢迎正在阅读的你也加入,一起分享。

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部