-1

why is this code invalid?. I don't know if I am using the lambda sintax correctly, but based on other posts, it looks fine.

struct foo{
    static int faa(int x);
};

int foo::faa(int x){
    bool isCon = []() {
       return true;
    };

    return isCon();
}
patzi
  • 353
  • 4
  • 13

1 Answers1

1

Lambdas have indeterminate type, so you can't know which type will one have. Obviously, the lambda you defined won't be of type bool (it may return a bool, but it's not one), so you would do this instead:

struct foo{
    static int faa(int x);
};

int foo::faa(int x){
    auto isCon = []()->bool {
       return true;
    };

    return isCon();
}

Here, the auto keyword tells the compiler to deduce the type for you. The ->bool expression tells the compiler that the lambda will return a bool.

However, your foo::faa() function returns an int, so a cast may ocurr because your lambda may return a bool (which has nothing to do with the question, but watch out).