My compiler, Visual Studio 19, says "Attempting to reference a deleted function":
class AClass
{public:
AClass(const AClass& other) = delete;
AClass() {}
AClass(AClass&& other) { }
AClass& operator=(const AClass& other) = delete;
AClass& operator=(AClass&& other) { return *this; }
int member;
};
void main()
{
AClass a;
AClass& aRef = a;
[=]() { return aRef.member; }; // "Attempting to reference a deleted function"
}
I assume it's trying to call the copy constructor, which is deleted, but I don't understand why as I'm trying to capture by reference by [=] value, meaning I don't copy the object AClass, I don't see how any copy is involved. My understanding is that the lambda looks something like this:
struct lambda {
AClass& refToAClass; // <--- this is the captured object
lambda(AClass& captureVariable) : refToAClass(captureVariable) {}
int operator()() const
{
return refToAClass.member;
}
};
void main()
{
// AND I CAN CONSTRUCT THE LAMBDA, NO COPYING OF AClass INVOLVED
AClass a;
AClass& aRef = a;
lambda lam(aRef); // WORKS FINE
}
How is a copy involved in this case? And how do I capture that reference?
[&]()instead of[=]()which is by value?