4

I've faced with problem while compiling lambda-function:

... (int level = 3) ...
QString str = [level] {QString s;for(int i=0;i++<level;s.append(" "));return s;};

Content of the error:

error: conversion from 'GainStatistic::getWidgetAndProps(QObject*, int)::<lambda()>' to non-scalar type 'QString' requested

I've tried this variant:

... (int level = 3) ...
QString str ([level] {QString s;for(int i=0;i++<level;s.append(" "));return s;});

error: no matching function for call to 'QString::QString(GainStatistic::getWidgetAndProps(QObject*, int)::<lambda()>)'

But lambda-expression in a function is simply value of some type? Is that right? Thus, QString(lambda-that-returns-QString) must call the QString::QString(const QString& ref) constructor and this must work:

... (int level = 3) ...
QString str([level] {const QString& ref = "123";return ref;}); //leads to the same error

Another variant:

QString str = [level]->QString {QString s;for(int i=0;i++<level;s.append(" "));return s;};

error: expected token ';' got 'str'

MinGW 4.6.1

3 Answers 3

13

You try to assign a lambda to a QString. What do you expect to happen? A lambda taking no arguments is a nullary function. You need to call it to get its return value.

e.g.

int x = [] { return 23; }();
                         ^^
                        call

Also, thanks for showing me the syntax for a no-argument lambda. I didn't know this was possible. I'm also a little unsure if it is really legal.

Edit: It is legal. 5.1.2

lambda-expression:
    lambda-introducer lambda-declarator{opt} compound-statement

lambda-declarator:
    (parameter-declaration-clause) mutable{opt}
4
  • 1
    It is legal. In 5.1.2.4: "If a lambda-expression does not include a lambda-declarator, it is as if the lambda-declarator were ()". May 7, 2012 at 9:29
  • Think it is legal, int main() { auto f = [] { return 23; }; return f(); }
    – dalle
    May 7, 2012 at 9:30
  • @jrok Thanks. I just confirmed that as well and added the relevant grammar productions.
    – pmr
    May 7, 2012 at 9:31
  • Thank you @pmr! I forgot about ()
    – Eddie R
    May 7, 2012 at 15:04
2

Lambda's are anonymous functions with the basic structure [capture](arguments){code}. You don't pass an argument to your lambda. I think you want

QString str = [](int x){QString s;for(int i=0;i++<x;s.append(" "));return s;} (level);
//            ^^ lambda starts here                         argument to lambda ^^   
0

From C++11 standard, 5.1.2.2 (shortened by me)

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type - called the closure type - whose properties are described below. [More text follows]

So, what others said, QString and the type of the lambda are incompatible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.