How are parameters referenced when using QTimer::singleShot?
-
Did you meant to write
QTimer::singleShot(200,this,[&,param](){ fun(param); });i.e. actually call the fun function from the lambda?If so, lambdas are immutable by default, which means
parambecomes const when copied by the capture group. You can't then use it as a non const parameter to fun. Couple of ways to fix this:-
If you don't want to modify str then make it a const parameter i.e.
void fun(const QString& str). -
If you want to modify the original parameter capture it by reference, not by value i.e.
[&](){ fun(param); }or explicitly[¶m](){ fun(param); } -
If you want to capture param by copy and modify that copy then you have to make the lambda mutable
[=]() mutable { fun(param); }or explicitly[param]() mutable { fun(param); }
There's usually no reason to do that, but if you really want to you can.
-
-
void fun(QString& str)
{}
QString param;
QTimer::singleShot(200,this,[&,param](){}
};
//error:qualifiers dropped in binding reference of type "QString &" to initializer of type "const QString"@mirro
Assuming something close to this is your actual code (since above won't compile), I can only guess there is an issue/confusion about passing both&("by-reference") andparam("not-by-reference"); do you really need this?Otherwise can you show actual code you are compiling.
-
Did you meant to write
QTimer::singleShot(200,this,[&,param](){ fun(param); });i.e. actually call the fun function from the lambda?If so, lambdas are immutable by default, which means
parambecomes const when copied by the capture group. You can't then use it as a non const parameter to fun. Couple of ways to fix this:-
If you don't want to modify str then make it a const parameter i.e.
void fun(const QString& str). -
If you want to modify the original parameter capture it by reference, not by value i.e.
[&](){ fun(param); }or explicitly[¶m](){ fun(param); } -
If you want to capture param by copy and modify that copy then you have to make the lambda mutable
[=]() mutable { fun(param); }or explicitly[param]() mutable { fun(param); }
There's usually no reason to do that, but if you really want to you can.
-