gpt4 book ai didi

检测 Callable 是否是静态的

转载 作者:行者123 更新时间:2023-12-01 12:39:39 27 4
gpt4 key购买 nike

我想我可以用 std.traits.functionAttributes 来做到这一点,但它不支持 static。对于任何类型的可调用对象(包含 opCall 的结构),我如何判断该可调用对象是否使用 static 注释?例如:

template isStaticCallable(alias fun) if (isCallable!fun)
{
enum isStaticCallable = ...?
}

最佳答案

Traits是 dlang 的一部分,它提供了对编译时信息的洞察力。可用特征之一是用作 __traits(isStaticFunction, fun)isStaticFunction

示例代码:

import std.traits;

template isStaticCallable(alias fun) if (isCallable!fun)
{
enum isStaticCallable = __traits(isStaticFunction, fun);
}


void main() {}

class Foo
{
static void boo() {}
void zoo() {}
}

pragma(msg, isStaticCallable!main); // note that this prints true because
// the function has no context pointer
pragma(msg, isStaticCallable!(Foo.boo)); // prints true
pragma(msg, isStaticCallable!(Foo.zoo)); // prints false

关于检测 Callable 是否是静态的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26189321/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com