在 MSVC 2012 中:
const std::string tableString;
std::vector<size_t> trPosVec;
// other stuff...
std::for_each(trIterator, endIterator,
[&, tableString, trPosVec](const boost::match_results<std::string::const_iterator>& matches){
trPosVec.push_back(std::distance(tableString.begin(), matches[0].second));
}
);
此代码给出了工具提示错误:
Error: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=size_t, _Alloc=std::allocator<char32_t>]" matches the argument list and object (the object has type qualifiers that prevent a match)
argument types are: (ptrdiff_t)
object type is: const std::vector<size_t, std::allocator<char32_t>>
我认为这意味着它是trPosVec
按价值捕获的。当我明确指定捕获模式时,它工作正常,[&tableString, &trPosVec]
. 如果我尝试双重指定 like [&, tableString, &trPosVec]
,它会给出Error: explicit capture matches default.
这里发生了什么?