我对isSomeFunction
的文档以及以下代码感到非常困惑:
static assert(!isFunctionPointer!(typeof(Object.toString))); // makes sense
static assert(!isDelegate!(typeof(Object.toString))); // what??
static assert( isSomeFunction!(typeof(Object.toString))); // what??
有人可以向我解释"函数"和"函数指针"之间的区别吗?
简短回答:
-
is(T == function)
T
是否为函数 -
isFunctionPointer!T
T
是否是函数指针(而不是委托) -
isDelegate!T
T
是否为代表 -
isSomeFunction!T
T
是函数、函数指针还是委托
长答案:
函数就是函数。
auto func(int val) {...}
它是一段代码,带有一个名称,您可以使用该名称调用它。你给它参数,它做它所做的一切,然后它返回一个结果。你可以打电话给它,但你不能传递它。为此需要一个函数指针。
函数指针是指向函数的指针。所以就像int
是一个int
,int*
是一个指向int
的指针,而int
不是指向int
的指针一样,函数也不是函数指针。如果需要函数指针,则需要指向函数的指针。语法与int
不同,但概念相同。
委托是带有状态的函数指针。例如,在
int foo(int value)
{
int bar()
{
return value + 5;
}
auto barDel = &bar;
return barDel();
}
void main()
{
auto fooFunc = &foo;
}
foo
是一个函数,bar
是一个嵌套函数,可以访问其外部作用域,barDel
是一个委托,因为它是一个带有状态(bar
有权访问的外部状态)的函数指针。如果将barDel
传递给另一个函数(或返回它),则会得到一个闭包(除非它传递给的函数通过scope
接受委托,在这种情况下,该函数保证委托不会逃脱其作用域),因为该状态需要放在堆上,以便它继续存在,即使其状态来自的函数调用在调用时已完成。 另一方面,funcFoo
是一个函数指针,因为它没有任何外部状态foo
。如果bar
static
,那么barDel
也将是一个函数指针而不是委托,因为bar
将不再有权访问它所在的函数(尽管它的主体将不得不改变,因为它将不再有权访问value
)。
现在,关于你的例子。 Object.toString
是 Object
的成员函数。所以,这是一个函数。它没有与之关联的状态。函数从来都做不到。其当前签名是
string toString();
但是因为它是Object
的成员函数,所以它的签名实际上是这样的
string toString(Object this);
this
作为参数传递给toString
。它不是与toString
关联的状态。所以,&Object.toString
不是代表。它只是一个函数指针。而且Object.toString
不是函数指针,所以即使&Object.toString
是委托,static assert(isDelegate!(typeof(Object.toString)))
仍然会失败,因为为了成为委托,它必须是函数指针,而事实并非如此。这是一个函数。
现在,不幸的是,typeof(&Object.toString)
被认为是string function()
而不是string function(Object)
,因此使用它来调用具有实际Object
的toString
需要一些工作。这是可以做到的,但我现在不记得怎么做(而且 IIRC 有点丑)。但无论如何,它都不会是代表,因为没有与之关联的状态。
如果你想要一个可以将Object
传递给它的函数并让它调用成员函数,那么你可以做一些类似的事情
auto obj = getObjectFromSomewhere();
auto func = function(Object obj){return obj.toString();};
auto result = func(obj);
如果要将对象与成员函数相关联,并且能够在该对象上调用该成员函数,而不必传递对象,则只需将其包装在委托中:
auto obj = getObjectFromSomewhere();
auto del = delegate(){return obj.toString();};
auto result = del();
这段代码应该总结事情并很好地说明事情:
int foo(int value)
{
int bar()
{
return value + 5;
}
static assert( is(typeof(bar) == function));
static assert(!isFunctionPointer!(typeof(bar)));
static assert(!isDelegate!(typeof(bar)));
static assert( isSomeFunction!(typeof(bar)));
auto barDel = &bar;
static assert(!is(typeof(barDel) == function));
static assert(!isFunctionPointer!(typeof(barDel)));
static assert( isDelegate!(typeof(barDel)));
static assert( isSomeFunction!(typeof(barDel)));
static int boz(int i)
{
return i + 2;
}
static assert( is(typeof(boz) == function));
static assert(!isFunctionPointer!(typeof(boz)));
static assert(!isDelegate!(typeof(boz)));
static assert(isSomeFunction!(typeof(boz)));
auto bozFunc = &boz;
static assert(!is(typeof(bozFunc) == function));
static assert( isFunctionPointer!(typeof(bozFunc)));
static assert(!isDelegate!(typeof(bozFunc)));
static assert( isSomeFunction!(typeof(bozFunc)));
return boz(bar());
}
static assert( is(typeof(foo) == function));
static assert(!isFunctionPointer!(typeof(foo)));
static assert(!isDelegate!(typeof(foo)));
static assert( isSomeFunction!(typeof(foo)));
void main()
{
auto fooFunc = &foo;
static assert(!is(typeof(fooFunc) == function));
static assert( isFunctionPointer!(typeof(fooFunc)));
static assert(!isDelegate!(typeof(fooFunc)));
static assert( isSomeFunction!(typeof(fooFunc)));
}
static assert( is(typeof(Object.toString) == function));
static assert(!isFunctionPointer!(typeof(Object.toString)));
static assert(!isDelegate!(typeof(Object.toString)));
static assert( isSomeFunction!(typeof(Object.toString)));
static assert(!is(typeof(&Object.toString) == function));
static assert( isFunctionPointer!(typeof(&Object.toString)));
static assert(!isDelegate!(typeof(&Object.toString)));
static assert( isSomeFunction!(typeof(&Object.toString)));
isSomeFunction
对于所有这些true
,因为它们都是函数、没有状态的函数指针或委托。
foo
、bar
、boz
和Object.toString
都是函数,所以它们true
用于is(T == function)
,但不适用于其他函数。
fooFunc
、bozFunc
和 &Object.toString
是没有状态的函数指针,因此它们true
用于isFunctionPointer!T
,但不适用于其他指针。
barDel
是代表,所以它对isDelegate!T
true
,但对其他人不。
希望这能为您解决问题。