Explicit Return Type of Lambda

后端 未结 3 1743
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 23:38

When I try and compile this code (VS2010) I am getting the following error: error C3499: a lambda that has been specified to have a void return type cannot return a va

3条回答
  •  隐瞒了意图╮
    2020-12-01 00:35

    The return type of a lambda (in C++11) can be deduced, but only when there is exactly one statement, and that statement is a return statement that returns an expression (an initializer list is not an expression, for example). If you have a multi-statement lambda, then the return type is assumed to be void.

    Therefore, you should do this:

      remove_if(rawLines.begin(), rawLines.end(), [&expression, &start, &end, &what, &flags](const string& line) -> bool
      {
        start = line.begin();
        end = line.end();
        bool temp = boost::regex_search(start, end, what, expression, flags);
        return temp;
      })
    

    But really, your second expression is a lot more readable.

提交回复
热议问题