C++ Lambda 的显式返回类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9620098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:05:08  来源:igfitidea点击:

Explicit Return Type of Lambda

c++visual-c++lambdac++11visual-c++-2012

提问by Ryan

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 value

当我尝试编译此代码 (VS2010) 时,出现以下错误: error C3499: a lambda that has been specified to have a void return type cannot return a value

void DataFile::removeComments()
{
  string::const_iterator start, end;
  boost::regex expression("^\s?#");
  boost::match_results<std::string::const_iterator> what;
  boost::match_flag_type flags = boost::match_default;
  // Look for lines that either start with a hash (#)
  // or have nothing but white-space preceeding the hash symbol
  remove_if(rawLines.begin(), rawLines.end(), [&expression, &start, &end, &what, &flags](const string& line)
  {
    start = line.begin();
    end = line.end();
    bool temp = boost::regex_search(start, end, what, expression, flags);
    return temp;
  });
}

How did I specify that the lambda has a 'void' return type. More-over, how do I specify that the lambda has 'bool' return type?

我如何指定 lambda 具有“void”返回类型。此外,如何指定 lambda 具有“bool”返回类型?

UPDATE

更新

The following compiles. Can someone please tell me why that compiles and the other does not?

以下编译。有人可以告诉我为什么编译而另一个没有?

void DataFile::removeComments()
{
  boost::regex expression("^(\s+)?#");
  boost::match_results<std::string::const_iterator> what;
  boost::match_flag_type flags = boost::match_default;
  // Look for lines that either start with a hash (#)
  // or have nothing but white-space preceeding the hash symbol
  rawLines.erase(remove_if(rawLines.begin(), rawLines.end(), [&expression, &what, &flags](const string& line)
  { return boost::regex_search(line.begin(), line.end(), what, expression, flags); }));
}

回答by Seth Carnegie

You can explicitly specify the return type of a lambda by using -> Typeafter the arguments list:

您可以-> Type在参数列表之后使用显式指定 lambda 的返回类型:

[]() -> Type { }

However, if a lambda has one statement and that statement is a return statement (and it returns an expression), the compiler can deduce the return type from the type of that one returned expression. You have multiple statements in your lambda, so it doesn't deduce the type.

但是,如果 lambda 有一个语句并且该语句是 return 语句(并且它返回一个表达式),则编译器可以从该返回表达式的类型推导出返回类型。您的 lambda 中有多个语句,因此它不会推断类型。

回答by Nicol Bolas

The return type of a lambda (in C++11) can be deduced, but onlywhen there is exactly one statement, and that statement is a returnstatement 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.

(在C ++ 11)的λ的返回类型可以推断,但有当正好一个语句,该语句是一个return语句返回表达式(初始化值列表不是表达,例如)。如果您有一个多语句 lambda,则假定返回类型为 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.

但实际上,您的第二个表达式更具可读性。

回答by Valen

You can have more than one statement when still return:

当仍然返回时,您可以有多个语句:

[]() -> your_type {return (
        your_statement,
        even_more_statement = just_add_comma,
        return_value);}

http://www.cplusplus.com/doc/tutorial/operators/#comma

http://www.cplusplus.com/doc/tutorial/operators/#comma