Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit Return Type of Lambda

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

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?

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); })); } 
like image 627
Ryan Avatar asked Mar 08 '12 15:03

Ryan


People also ask

What is the return type of lambda function?

The return type for a lambda is specified using a C++ feature named 'trailing return type'. This specification is optional. Without the trailing return type, the return type of the underlying function is effectively 'auto', and it is deduced from the type of the expressions in the body's return statements.

What is the return type of lambda exception?

If Lambda encounters an error, it returns an exception type, message, and HTTP status code that indicates the cause of the error.

Can lambda return Boolean?

The compiler notes that the lambda satisfies this functional interface's boolean accept(File pathname) method (both have a single parameter and the lambda body returns a Boolean value) and binds the lambda to FileFilter .

What is the return type of lambda expression in Java 8?

The body of a lambda expression can contain zero, one or more statements. When there is a single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.


1 Answers

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

[]() -> 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.

like image 140
Seth Carnegie Avatar answered Sep 21 '22 12:09

Seth Carnegie