d的typeof问题

原文

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());
posted @ 2022-08-22 14:45  zjh6  阅读(17)  评论(0)    收藏  举报  来源