Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 检查符号是否为D中的成员函数/字段的[指针] 在C++中,我们可以很容易地检测到给定类型是指向成员函数/字段的指针: 模板 结构是_成员_指针:std::false_类型{}; 模板 结构是_成员_指针:std::true_类型{};_Templates_D - Fatal编程技术网

Templates 检查符号是否为D中的成员函数/字段的[指针] 在C++中,我们可以很容易地检测到给定类型是指向成员函数/字段的指针: 模板 结构是_成员_指针:std::false_类型{}; 模板 结构是_成员_指针:std::true_类型{};

Templates 检查符号是否为D中的成员函数/字段的[指针] 在C++中,我们可以很容易地检测到给定类型是指向成员函数/字段的指针: 模板 结构是_成员_指针:std::false_类型{}; 模板 结构是_成员_指针:std::true_类型{};,templates,d,Templates,D,D中没有这样的语法(我的意思是tu::*)。此外,自由函数和方法具有相同的类型: void func(); 结构 { int x; void func(){} } 静态断言(is(typeof(func)==typeof(S.func)); 所以,我的问题是:我们能用D写一个类似于C++版本的模板吗 模板是的成员(别名M) { 别名T=/**/; } 静态断言(is(is_成员_of!(S.func)==S)); 静态断言(is(是!func==void的_成员_)); 静态断言(is(is_

D中没有这样的语法(我的意思是
tu::*
)。此外,自由函数和方法具有相同的类型:

void func();
结构
{
int x;
void func(){}
}
静态断言(is(typeof(func)==typeof(S.func));
所以,我的问题是:我们能用D写一个类似于C++版本的模板吗

模板是的成员(别名M)
{
别名T=/**/;
}
静态断言(is(is_成员_of!(S.func)==S));
静态断言(is(是!func==void的_成员_));
静态断言(is(is_成员_of!(S.x)==S));

尝试通过
init
对象获取函数的地址:

static assert(is(typeof(&func) == typeof(&S.init.func)));
将提供:

ooo.d(9): Error: static assert:  is(void function() == void delegate()) is false
成员函数(除非是静态的,但它不是真正的成员函数)将被键入
delegate
,因为它需要
this
对象,而另一个将被键入
function

这将对函数起作用,您可以对变量做类似的操作(提示:
static assert:is(typeof(&x))为false
,但是
static assert(is(typeof(&S.init.x));
传递-请注意.init),如果您想知道它是否是一个实际的成员,请使用运行时
要求(也就是说,不是静态的

我将把这些信息转换成模板检查作为一个练习留给读者(提示:
is(typeof(something)==delegate)
是语言中的一种东西……)

但是,如果您只想知道符号上是否有父类型,有一种不同的方法:只需询问编译器是否有父类型!好的,我承认代码稍微长一些,以获得静态断言所寻找的
void
响应,但不多:

// identity template to hack around parser limitations
// this is common when using the __traits stuff, alas.
alias I(alias T) = T;

template is_member_of(alias M) {
        // if it has a parent, return it, otherwise, return void
        static if(is(I!(__traits(parent, M))))
                alias is_member_of = I!(__traits(parent, M));
        else
                alias is_member_of = void;
}
您还可以进一步检查父对象是否是某些东西,如struct或class,但实际上,如果它确实存在,则可能就是您要查找的对象