"功能"与"功能指针" - 有什么区别?

Meh*_*dad 4 d function-pointers function

对文档isSomeFunction以及以下代码感到困惑:

static assert(!isFunctionPointer!(typeof(Object.toString)));  // makes sense
static assert(!isDelegate!(typeof(Object.toString)));         // what??
static assert( isSomeFunction!(typeof(Object.toString)));     // what??
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释"功能"和"功能指针"之间的区别吗?

Jon*_*vis 6

简答:

  • is(T == function)      是否T是一个功能
  • isFunctionPointer!T  是否T是函数指针(而不是委托)
  • isDelegate!T                是否T是代表
  • isSomeFunction!T        无论T是函数,函数指针还是委托

答案很长:

一个功能就是一个功能.

auto func(int val) {...}
Run Code Online (Sandbox Code Playgroud)

这是一个带有名称的代码块,您可以使用该名称调用该名称.你给它参数,它做它做的任何事情,它返回一个结果.你可以打电话给它,但你无法传递它.你需要一个函数指针.

函数指针是指向函数的指针.所以就像intintint*是一个指针int,并且int不是一个指针int,函数是不是一个函数指针.如果需要函数指针,则需要指向函数的指针.语法与int它的不同,但它是相同的概念.

委托是具有状态的函数指针.所以,例如,在

int foo(int value)
{
    int bar()
    {
        return value + 5;
    }

    auto barDel = &bar;

    return barDel();
}

void main()
{
    auto fooFunc = &foo;
}
Run Code Online (Sandbox Code Playgroud)

foo是一个函数,bar是一个嵌套函数,它可以访问它的外部作用域,并且barDel是一个委托,因为它是一个带状态的函数指针(bar可以访问的外部状态).如果你传递barDel给另一个函数(或返回它),你将获得一个闭包(除非传递给它的函数接受委托scope,在这种情况下该函数保证委托不会逃避其范围),因为该状态需要放在堆上,以便它继续存在,即使它调用状态来自的函数调用已经完成.funcFoo另一方面,是一个函数指针,因为foo没有任何外部状态.如果barstatic,那么barDel也将是一个函数指针而不是委托,因为bar它将不再能够访问它所在的函数(尽管它的主体必须被更改,因为它将不再具有访问权限value).

现在,就你的例子而言.Object.toString是...的成员函数Object.所以,这是一个功能.它没有与之相关的州.功能永远不会.它目前的签名是

string toString();
Run Code Online (Sandbox Code Playgroud)

但因为它是一个成员函数Object,它的签名就像是

string toString(Object this);
Run Code Online (Sandbox Code Playgroud)

this传递给toString作为参数.它与州无关toString.所以,&Object.toString不是代表.它只是一个函数指针.并且Object.toString不是函数指针,所以即使&Object.toString 委托,static assert(isDelegate!(typeof(Object.toString)))仍然会失败,因为为了成为委托,它必须是一个函数指针,它不是.这是一个功能.

不幸的typeof(&Object.toString)是,现在被认为是,string function()而不是string function(Object),所以使用它来toString实际调用Object需要一些工作.它可以完成,但我不记得此刻(并且它有点丑陋的IIRC).但它不会是一个代表,因为没有与之相关的州.

如果你想要一个可以传递Object给它的函数并让它调用一个成员函数,那么你可以做类似的事情

auto obj = getObjectFromSomewhere();
auto func = function(Object obj){return obj.toString();};
auto result = func(obj);
Run Code Online (Sandbox Code Playgroud)

如果要将对象与成员函数关联,并且能够在该对象上调用该成员函数而不必传递该对象,那么您只需将其包装在委托中:

auto obj = getObjectFromSomewhere();
auto del = delegate(){return obj.toString();};
auto result = del();
Run Code Online (Sandbox Code Playgroud)

这段代码应该总结一下并很好地说明事情:

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)));
Run Code Online (Sandbox Code Playgroud)

isSomeFunction适用true于所有这些,因为它们都是函数,没有状态的函数指针或委托.

foo,bar,boz,和Object.toString全功能的,所以他们trueis(T == function)而不是别人.

fooFunc,bozFunc&Object.toString是函数指针没有状态,所以他们trueisFunctionPointer!T而不是别人.

barDel是一个代表,所以它是true为了isDelegate!T而不是为其他人.

希望这能为您解决问题.