以c++ 0x为单位复制引用变量

时间:2021-09-11 18:59:15

According to the answers and comments for this question, when a reference variable is captured by value, the lambda object should make a copy of the referenced object, not the reference itself. However, GCC doesn't seem to do this.

根据这个问题的答案和注释,当引用变量被值捕获时,lambda对象应该复制引用对象,而不是引用本身。然而,GCC似乎并没有这么做。

Using the following test:

使用以下测试:

#include <stddef.h>
#include <iostream>

using std::cout;
using std::endl;

int main(int argc, char** argv)
{
    int i = 10;
    int& ir = i;

    [=]
    {
        cout << "value capture" << endl
             << "i: " << i << endl
             << "ir: " << ir << endl
             << "&i: " << &i << endl
             << "&ir: " << &ir << endl
             << endl;
    }();

    [&]
    {
        cout << "reference capture" << endl
             << "i: " << i << endl
             << "ir: " << ir << endl
             << "&i: " << &i << endl
             << "&ir: " << &ir << endl
             << endl;
    }();    

    return EXIT_SUCCESS;
}

Compiling with GCC 4.5.1, using -std=c++0x, and running gives the following output:

使用GCC 4.5.1编译,使用-std=c++0x,运行得到如下输出:

value capture
i: 10
ir: -226727748
&i: 0x7ffff27c68a0
&ir: 0x7ffff27c68a4

reference capture
i: 10
ir: 10
&i: 0x7ffff27c68bc
&ir: 0x7ffff27c68bc

When captured by copy, ir just references junk data. But it correctly references i when captured by reference.

当被复制捕获时,ir仅仅引用垃圾数据。但是当它被引用捕获时,它正确地引用了i。

Is this a bug in GCC? If so, does anyone know if a later version fixes it? What is the correct behavior?

这是GCC中的一个bug吗?如果是的话,有人知道以后的版本会不会修复它吗?正确的行为是什么?

EDIT

If the first lambda function is changed to

如果第一个函数被改成

[i, ir]
{
    cout << "explicit value capture" << endl
         << "i: " << i << endl
         << "ir: " << ir << endl
         << "&i: " << &i << endl
         << "&ir: " << &ir << endl
         << endl;
}();

then the output looks correct:

输出看起来是正确的:

explicit value capture
i: 10
ir: 10
&i: 0x7fff0a5b5790
&ir: 0x7fff0a5b5794

This looks more and more like a bug.

这看起来越来越像一个bug。

2 个解决方案

#1


7  

This has just been fixed in gcc-4.7 trunk and gcc-4.6 branch. These should be available in gcc-4.7.0 (a while from now - still in stage 1) and gcc-4.6.2 (alas 4.6.1 just came out.)

这刚刚在gcc-4.7主干和gcc-4.6分支中得到了修正。这些应该可以在gcc-4.7.0和gcc-4.6.2中使用(唉,4.6.1刚刚发布)。

But the intrepid could wait for the next snapshots or get a subversion copy.

但勇敢者可以等待下一个快照或获得一个subversion副本。

See audit trail for details.

有关详细信息,请参见审计跟踪。

#2


4  

Compiled with VS 2010 gives:

与VS 2010汇编:

value capture
i: 10
ir: 10
&i: 0012FE74
&ir: 0012FE78

reference capture
i: 10
ir: 10
&i: 0012FF60
&ir: 0012FF60

Looks like a bug for me.

对我来说,这似乎是件坏事。

#1


7  

This has just been fixed in gcc-4.7 trunk and gcc-4.6 branch. These should be available in gcc-4.7.0 (a while from now - still in stage 1) and gcc-4.6.2 (alas 4.6.1 just came out.)

这刚刚在gcc-4.7主干和gcc-4.6分支中得到了修正。这些应该可以在gcc-4.7.0和gcc-4.6.2中使用(唉,4.6.1刚刚发布)。

But the intrepid could wait for the next snapshots or get a subversion copy.

但勇敢者可以等待下一个快照或获得一个subversion副本。

See audit trail for details.

有关详细信息,请参见审计跟踪。

#2


4  

Compiled with VS 2010 gives:

与VS 2010汇编:

value capture
i: 10
ir: 10
&i: 0012FE74
&ir: 0012FE78

reference capture
i: 10
ir: 10
&i: 0012FF60
&ir: 0012FF60

Looks like a bug for me.

对我来说,这似乎是件坏事。