const
ラムダ式で参照によってキャプチャすることは可能ですか?
たとえば、以下にマークされた割り当てを失敗させたい:
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string strings[] =
{
"hello",
"world"
};
static const size_t num_strings = sizeof(strings)/sizeof(strings[0]);
string best_string = "foo";
for_each( &strings[0], &strings[num_strings], [&best_string](const string& s)
{
best_string = s; // this should fail
}
);
return 0;
}
更新:これは古い質問であるため、これを支援する機能が C++14 にある場合は更新することをお勧めします。C++14 の拡張機能により、const 参照によって非 const オブジェクトをキャプチャできますか? ( 2015年8月)