I have the following test code:
#include <iostream>
#include <string>
void printValue(std::string&& val)
{
std::cout << "Got value: " << val << "\n";
}
int main() {
std::string testValue = "Test Value";
auto lambda = [testValue = std::move(testValue)]() mutable
{
printValue(std::move(testValue));
};
lambda();
lambda();
lambda();
}
I get the result:
Got value: Test Value
Got value: Test Value
Got value: Test Value
It it a valid assumption that moving an object from within lambda that has been move captured will always have the initial state that it was moved into the lambda with, or is this just an artifact of an object being in "valid but unspecified state"?
std::move
gives permission to move. It doesn't do anything else. The client who receives such a "moved" value may choose to move from it, or may not. In your exampleprintValue
chooses not to move fromval
. – Overprint