http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58972
Bug ID: 58972
Summary: Lambda can't access private members
Product: gcc
Version: 4.9.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: akim.demaille at gmail dot com
Hi all,
Again, this problem report might be bogus. IANALL and reading the proposed
standard did not really help me understand if the problem is in the compiler,
or in the eye of the beholder.
Anyway, I think that if this is not a problem of G++, it is one of C++. At
least Clang++ (3.4) accepts it.
$ cat foo.cc
class base
{
protected:
using type = int;
};
template <typename T>
class derive: public base
{
using typename base::type;
public:
void foo(type a)
{
auto f = [a](type x){ return x == a; };
f(32);
}
};
int main()
{
derive<char> d;
d.foo(23);
}
$ clang++-mp-3.4 -std=c++11 foo.cc
$ g++-mp-4.9 --version
g++-mp-4.9 (MacPorts gcc49 4.9-20130915_0) 4.9.0 20130915 (experimental)
Copyright (C) 2013 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.
$ g++-mp-4.9 -std=c++11 foo.cc
foo.cc: In instantiation of 'struct derive<T>::foo(base::type) [with T = char;
base::type = int]::<lambda(using type = int)>':
foo.cc:14:42: required from 'void derive<T>::foo(base::type) [with T = char;
base::type = int]'
foo.cc:22:11: required from here
foo.cc:4:19: error: 'using type = int' is protected
using type = int;
^
foo.cc:14:18: error: within this context
auto f = [a](type x){ return x == a; };
^
$ g++-mp-4.8 --version
g++-mp-4.8 (MacPorts gcc48 4.8.1_3) 4.8.1
Copyright (C) 2013 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.
$ g++-mp-4.8 -std=c++11 foo.cc
foo.cc: In instantiation of 'struct derive<T>::foo(base::type) [with T = char;
base::type = int]::__lambda0':
foo.cc:14:42: required from 'void derive<T>::foo(base::type) [with T = char;
base::type = int]'
foo.cc:22:11: required from here
foo.cc:4:19: error: 'using type = int' is protected
using type = int;
^
foo.cc:14:18: error: within this context
auto f = [a](type x){ return x == a; };
^
FWIW, if derived is no longer template, it works.
BTW, someone should have a look at this:
http://stackoverflow.com/questions/11933999/why-is-it-not-possible-to-use-private-method-in-a-lambda
(and especially http://ideone.com/DJezf). Maybe it's the same issue, maybe
it's slightly different, but again, clang++ eats it.