I have an understanding problem with C++11, Visual Studio 2013 (probably not a Visual Studio problem) and non-static data member initializers of lambda function members.
class Test {
private:
int _number;
int _numberGetter() {
return this->_number; // OK
}
public:
std::function<int(void)> NumberTest1 = [&]()->int {
return this->_number; // error: 'this' cannot be used inside the body of this lambda
};
//std::function<int(void)> NumberTest2 = Test::_numberGetter;
std::function<int(void)> NumberTest3;
std::function<int(void)> NumberTest4;
std::function<int(void)> NumberTest5;
std::function<int(void)> NumberTest6;
Test() : NumberTest3(&Test::_numberGetter) { } // OK
Test() : NumberTest4([&]()->int {
return this->_number; // OK
}) { }
Test() {
NumberTest5 = &Test::_numberGetter; // OK
}
Test() {
NumberTest6 = [&]()->int {
return this->_number; // OK
};
}
};
Why can't I directly initialize the std::function NumberTest1 with a lambda function, in which the object instance this is.
Maybe it's because the lambda function will be directly complied, other then the normal member function _numberGetter, and in this moment the instance object this is not avalible. But this is only a presumption. Against this presumption is that NumberTest1 is not a constant and should only avalible from a class instance.
Maybe this is possible and I have only a little mistake in my code.
EIDT 1:
Visual Studio Version:
Microsoft Visual Studio Ultimate 2013
Version 12.0.31101.00 Update 4
.Net Version 4.5.51641
Aucun commentaire:
Enregistrer un commentaire