Open dabro opened 7 years ago
Can you run the same input by prefixing it:
.rawInput 1
// code...
.rawInput 0
Do you still see the same behavior?
Didn't think of that, ok. Unfortunately, still fails proper behavior:
In [5]: auto k3 = make_kitten2(2); Out[5]: ((lambda) &) @0x11aef4000
In [6]: auto k4 = make_kitten2(2); Out[6]: ((lambda) &) @0x11aef4188
In [7]: k3(4) //&a = 0x11aef429c, and &b = 0x11aef4298 (int) 8 Out[7]: (int) 8
In [8]: k4(4); //&a = 0x11aef4f24, and &b = 0x11aef4f20 Out[8]: (int) 8
In [9]: k3(4); //&a = 0x11aefd06c, and &b = 0x11aefd068 Out[9]: (int) 8
In [10]: k3(4); //&a = 0x11aefd1ac, and &b = 0x11aefd1a8 Out[10]: (int) 8
The following is from Arthur O'Dwyer's 2016 Cppcon "Lambda's from First Principles"
(forgive my weird formatting)
The
static int a
andb
should both be shared between the individual instancesk3
andk4
of the same lambda. Meaning that each call, regardless of which lambda is called, should incrementa
andb
.In the code above, neither the static
a
from themake_kittens()
scope is mutating, nor isb
from the lambda scope. O'Dwyer's video said thatb
should be shared amongst all the lambdas produced by this function; clang showsk3
andk4
are the same (anonymous) type, ie two object instances of the same class, so they share the sameoperator()
and statics of the underlying function object class...Of note, cling is allowing the lambda to access
a
andb
, despite not capturing, which is proper non-automatic-storage handling. It is also throwing no errors about mutating them despite nomutable
keyword, which is also correct.Cling shows each
a
andb
in eachk3
andk4
call as distinct. XCode shows the addresses being identical: