定义lambda时[&]是什么意思?

鲁济姆

方法参考文档中,有一些示例代码,复制如下:

char8 GetNext()
{
    if (i >= str.Length)
        return 0;
    return str[i++];
}

/* Allocates a delegate bound to GetNext() */
delegate char8() strDlg = scope => GetNext;

strDlg = scope () =>
{
    return 'A';
};

strDlg = scope [&] () =>
{
    return GetNext();
};

/* This delegate owns a string */
String tempStr = new String(str);
tempStr.EnsureNullTerminated();
strDlg = scope [&] () =>
{
    return str[i++];
}
~
{
    delete tempStr;
};

在三个lambda定义示例中,有[&]两个在分配表达式(scope)和lambda的参数之间存在一个那是[&]什么

对于其他示例,这是来自IDE源中包含的测试的一些摘录

struct Splattable
{
    public int32 mA = 10;
    public int16 mB = 200;

    public void TestLambda() mut
    {
        delegate int(ref int a, ref int b) dlg = scope [&] (a, b) => 
        {
            a += 20;
            b += 30;
            mA++;
            return mA + a + b;
        };

        mA = 100;
        int testA = 8;
        int testB = 9;
        Test.Assert(dlg(ref testA, ref testB) == 100+1 + 20+8 + 30+9);
        Test.Assert(testA == 28);
        Test.Assert(testB == 39);
        Test.Assert(mA == 101);
    }
}

class ClassA
{
    public int mA;

    public void TestLambda()
    {
        delegate int(ref int a, ref int b) dlg = scope (a, b) => 
        {
            a += 20;
            b += 30;
            mA++;
            return mA + a + b;
        };

        mA = 100;
        int testA = 8;
        int testB = 9;
        Test.Assert(dlg(ref testA, ref testB) == 100+1 + 20+8 + 30+9);
        Test.Assert(testA == 28);
        Test.Assert(testB == 39);
        Test.Assert(mA == 101);
    }
}

这是源代码的IDEHelper部分的另一组示例/测试其中每个lambda定义都具有[&]

using System;

namespace Tests
{
    class Lambdas
    {
        [Test]
        static void TestBasics()
        {
            int a = 1;

            Action act = scope [&] () =>
            {
                Action act2 = scope [&] () =>
                {
                    a += 100;
                };
                act2();
            };
            act();

            Test.Assert(a == 101);
        }

        static int Add3<T>(T func) where T : delegate int()
        {
            return func() + func() + func();
        }

        [Test]
        static void TestValueless()
        {
            Test.Assert(Add3(() => 100) == 300);

            int a = 20;
            int result = Add3(() => a++);
            Test.Assert(result == 63);
        }

        [Test]
        static void LambdaWithDtor()
        {
            int a = 10;
            int b = 20;

            //
            {
                delegate void() dlg = scope [&] () =>
                {
                    a++;
                }
                ~
                {
                    b++;
                };
                dlg();
            }

            Test.Assert(a == 11);
            Test.Assert(b == 21);

            delegate void() dlg = new [&] () =>
            {
                a += 100;
            }
            ~
            {
                b += 200;
            };
            dlg();
            Test.Assert(a == 111);
            Test.Assert(b == 21);
            delete dlg;
            Test.Assert(b == 221);
        }
    }
}

最后,[&]严格来说可能不是lambda。我可以将其放在其他定义中,例如let s = new [&] String();,它可以毫无问题地编译,并且据我所知,运行与没有它时具有相同的结果。但是,我尚未在示例/测试代码中的任何其他地方看到它。

艾萨克·保罗

[&]手段通过引用而不是值来捕获任何引用的本地。

在示例中,对于ClassA,this将其按值传递给lambda。有效:this.mA++;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章