This is quite a strange error to me. Check the code below:
void test(void){
vector<string> v;
v.push_back("hello");
auto fn=[=](){
v.push_back("world");
};
}
The first push_back method passed the compilation but the second failed, yielding the error:
Error:no matching member function for call to 'push_back'
The compiler note is:
**Note:(687, 36) candidate function not viable: 'this' argument has type 'const vector' (aka 'const vector, allocator > >')
But the method is not marked const**
.
Well I am not using any const argument and I cannot figure what the compiler is trying to tell me. Could anybody help me?
To fix this error during the method call, we must provide adequate corresponding parameters to the function. The other way of resolving this error is to make different overloaded functions have different parameters. Here we just modify the argument of the function main().
Lambdas can both capture variables and accept input parameters. A parameter list (lambda declarator in the Standard syntax) is optional and in most aspects resembles the parameter list for a function. auto y = [] (int first, int second) { return first + second; };
C++ 11 introduced lambda expressions to allow inline functions which can be used for short snippets of code that are not going to be reused and therefore do not require a name. In their simplest form a lambda expression can be defined as follows: [ capture clause ] (parameters) -> return-type { definition of method }
C++ Lambda expression allows us to define anonymous function objects (functors) which can either be used inline or passed as an argument. Lambda expression was introduced in C++11 for creating anonymous functors in a more convenient and concise way.
Lambda call operator member functions are const
by default. If you want a mutable call operator, say mutable
:
auto fn = [=]() mutable {
// ^^^^^^^
v.push_back("world");
};
Having const
be the default forces you to be explicit about the fact that you mean to capture a copy of the vector and mutate that copy, rather than the original vector v
.
By contrast, variables that are captured by reference can be mutated by const-qualified member functions:
auto fn = [&]() {
// ^^^
v.push_back("world"); // modifies original "V"!
};
(This is essentially because const T
is the same as T
when T = U &
; there are no "constant references" in C++.)
capture by value is const
use the keyword mutable
(doesn't change the original vector):
auto fn = [=]() mutable {
v.push_back("world");
};
or by reference (changes the original vector):
auto fn = [&]() {
v.push_back("world");
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With