C++ lambdas, "error: expected expression"

Phoenix

I'm trying to reproduce java's streams API in C++, and have made this program so far.

#include <iostream>

using namespace std;

template <typename E>
class stream {
    virtual void collect(void (*consumer) (E)) = 0;
    virtual bool anyMatch(bool (*predicate) (E)) {
        bool found = false;
        collect([&found](E obj) -> { if (predicate(obj)) {found = true} });
        return found;
    }
};

int main() {
    return 0;
}

But when I try to compile it with g++:

enter image description here

What am I doing wrong the lambda? It is supposed to provide a function (the consumer) which will test the given E with the predicate (a function), and if it yields true, set found to true.

Vittorio Romeo

There are a few separate issues in your code:

  1. The error you posted suggests that you're compiling in pre-C++11 mode. Lambdas were introduced in C++11.

  2. There are several syntax errors. Your lambda trailing return type is missing the type, and you're missing a semicolon in the body of the lambda.

  3. You're trying to convert a non-captureless lambda to a function pointer. This is impossible, as capturing variables requires a state/context.


Your code is not valid C++ - the syntax is incorrect. Here's a version with valid syntax:

template <typename E>
class stream
{
    virtual void collect(void (*consumer)(E)) = 0;
    virtual bool anyMatch(bool (*predicate)(E))
    {
        bool found = false;
        collect([predicate, &found](E obj)
            {
                if(predicate(obj))                    
                    found = true;                    
            });

        return found;
    }
};

Nevertheless, the code will not compile because non-captureless lambdas cannot be converted to function pointers. If that was allowed, it would be a recipe for disaster, as the information regarding the captured variables will be lost. You can instead use std::function, which erases the type of the lambda and works with non-empty capture lists, at the cost of memory/run-time overhead:

template <typename E>
class stream
{
    virtual void collect(std::function<void(E)> consumer)
    {
        (void)consumer;
    }

    virtual bool anyMatch(std::function<bool(E)> predicate)
    {
        bool found = false;
        collect([predicate, &found](E obj)
            {
                if(predicate(obj))                    
                    found = true;                    
            });

        return found;
    }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C - Error: expected expression before int

From Dev

C error: Expected expression before int

From Dev

c error: expected expression before ‘{’ token

From Dev

C error: expected expression before '||' token

From Dev

C _Generic error - expected expression before '_Bool'

From Dev

C error: expected expression before '||' token

From Dev

Initialize an array in C error "expected expression before ‘]’ token"

From Dev

Getting an error "error: expected expression before..." when I try to call a function with parameters in C

From Dev

error: expected expression before ‘float’

From Dev

error: expected expression before '{' token|

From Dev

Syntax Error: expected expression, got '<='

From Dev

56:68: error: expected expression

From Dev

Char array error, expected an expression, error

From Dev

javascript syntax error expected expression, got '<'

From Dev

error: expected expression; use of undeclared identifier

From Dev

Syntax Error:expected expression got end of script

From Dev

Why is the compiler showing error "expected expression before stud"?

From Dev

Why is this program showing error expected expression before 's'

From Dev

Error : Expected expression before 'DATA /* : typedef struct DATA DATA */

From Dev

Lambdas in C++03

From Dev

C++ lambdas passing

From Dev

Emulating lambdas in C?

From Dev

Type of lambdas in C++

From Dev

C++ lambdas passing

From Java

C# 7 tuples and lambdas

From Dev

C# make a Dictionary of Lambdas

From Dev

Combining two lambdas in C++

From Dev

How to unmangle mangled names of C++ lambdas?

From Dev

Converting ObjC Blocks to C# Lambdas