Bug 51927 - [C++0x] Cannot access non-static members in initializer
Summary: [C++0x] Cannot access non-static members in initializer
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda, rejects-valid
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2012-01-21 00:30 UTC by gccearlyadopter
Modified: 2022-03-11 00:32 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.0, 4.9.0
Known to fail: 4.7.1
Last reconfirmed: 2012-05-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description gccearlyadopter 2012-01-21 00:30:43 UTC
GCC47 doesn't allow to access non-static members in non-static member initialization. The following program fails to compile with the error "invalid use of non-static data member 'testee::l1'".

#include <iostream>
#include <functional>
using std::cout;
using std::endl;

struct testee
{
    std::function<void()> l1 = []()
    {   
        cout << "world" << endl;
    };  
    std::function<void()> l2 = [=]()
    {   
        cout << "hello " << endl;
        l1();
    };  
};

int main()
{
    testee t;
    t.l2();
}

Taking the address of l1 in the lambda expression used to initialize l2 is also refused by GCC.
Comment 1 gccearlyadopter 2012-05-30 07:48:35 UTC
This Bug still exists in the latest GCC 4.7 release.
Comment 2 Jonathan Wakely 2012-05-30 08:38:03 UTC
4.7.0 *is* the latest GCC release :)

Anyway, confirmed, the example looks valid to me.
Comment 3 gccearlyadopter 2012-05-30 12:34:12 UTC
My local GCC says "4.7.0_3". :)
Comment 4 js 2012-12-05 17:29:35 UTC
still seems to be broken in 4.7.2 :-P

but is known to be working with llvm 3.2

# make
simple.cpp: In lambda function:
simple.cpp:11:5: error: invalid use of non-static data member ‘testee::l1’
simple.cpp:15:9: error: from this location
make[2]: *** [CMakeFiles/simple.dir/simple.cpp.o] Error 1
make[1]: *** [CMakeFiles/simple.dir/all] Error 2
make: *** [all] Error 2
*** Failed ***

# g++ --version
g++ (Ubuntu/Linaro 4.7.2-11precise2) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Comment 5 Paolo Carlini 2013-05-04 12:20:56 UTC
This is fixed. I'm adding the testcase and closing the report.
Comment 6 Paolo Carlini 2013-05-04 12:33:13 UTC
Done.
Comment 7 js 2013-05-06 11:37:54 UTC
which gcc version will have that patch applied?

thanks for fixing, hope libcppa will work with gcc now again!

On 05/04/2013 02:33 PM, paolo.carlini at oracle dot com wrote:
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51927
>
> Paolo Carlini <paolo.carlini at oracle dot com> changed:
>
>             What    |Removed                     |Added
> ----------------------------------------------------------------------------
>               Status|NEW                         |RESOLVED
>               Blocks|                            |54367
>           Resolution|                            |FIXED
>
> --- Comment #6 from Paolo Carlini <paolo.carlini at oracle dot com> 2013-05-04 12:33:13 UTC ---
> Done.
>