Given the following program:
#include <iostream>
#include <memory>
using namespace std;
int main() {
std::shared_ptr<int> i(new int(42));
cout << i.use_count() << endl;
auto fn = [=](){i; cout << 42 << endl;};
cout << i.use_count() << endl;
return 0;
}
When the compiler decides which objects it will capture?
The value of shared_ptr is inever used in a lambda expression. Therefore, in a normal function, I would suggest that the optimizer removes this nop statement.
But if it is removed, the compiler might think that there is ino need to write.
Thus, with gcc, this program will always output 1.2 as output.
But is this guaranteed by the standard?
mkaes source
share