使用GCC 6.1,以下程序:
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
int main()
{
static const std::string foo {"foo"};
std::vector<std::string> bars {{""}};
std::cout << "foo outside: " << foo << std::endl;
std::for_each(std::cbegin(bars), std::cend(bars), [] (const auto& bar) {
std::cout << "foo inside: " << foo << std::endl;
});
}
打印:
foo outside: foo
foo inside:
发生了什么?