"Functions"与"function pointers" - 有什么区别?



我对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是一个intint*是一个指向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.toStringObject 的成员函数。所以,这是一个函数。它没有与之关联的状态。函数从来都做不到。其当前签名是

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),因此使用它来调用具有实际ObjecttoString需要一些工作。这是可以做到的,但我现在不记得怎么做(而且 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,因为它们都是函数、没有状态的函数指针或委托。

foobarbozObject.toString都是函数,所以它们true用于is(T == function),但不适用于其他函数。

fooFuncbozFunc&Object.toString没有状态的函数指针,因此它们true用于isFunctionPointer!T,但不适用于其他指针。

barDel是代表,所以它对isDelegate!T true,但对其他人不。

希望这能为您解决问题。