Lambda in C++0x: error: conversion from to non-scalar type requested
c++, lambda, qt
Solution
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}
Problem
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