1

在 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.这里发生了什么?

4

1 回答 1

6

Your capture specification indicates that you want to capture all local variables by reference, except tableString and trPosVec, which you want to capture by value. If these two variables are the only variables you want to capture, and you want to capture them by reference, you should use the capture expression, [&tableString, &trPosVec], or simply capture all local variables by reference, [&].

于 2013-08-24T16:39:55.413 回答