Mutable lambda in C++














































Mutable lambda in C++




This article is based on C++11.

This article is based on the lambda function. The audience must be familiar with the C++11 lambda function.

Object captured (capture clause) in lambda are immutable by default. Because the operator() of the generated functor which is const by default. Mutable lambda allows the body to modify the parameters captured by copy, and to call their non-const member functions.

Mutable lambda will modify the variable in the scope of lambda function only, but it will be propagated to the outside of the lambda function.

To define mutable lambda function, you need to use mutable keyword.

Example:

#include <iostream>

using namespace std;

int main() {

// When we are passing local variable by value to lambda function
// then it does not allow to chnage it.
// If we pass the variable by reference then it will update the
// variable permanently.
//
// The concept of the mutable lambda is like as scope resolution
// You can update the local variable and its updated value will
// persist only across the lambda function.
//
// After coming out of the lambda function, variable will have
// older value only.

// You meed to use mutable keyword with lambda function.

int x = 11;

// Passing variable x by value to the lambda function
[x]() mutable{
x = 22;
cout << "Inside lambda function, x = " << x << endl;
}();

cout << "Outside lambda function, x = " << x << endl;

return 0;
}

Output:

Inside lambda function, x = 22
Outside lambda function, x = 11


More Articles of Pankaj Choudhary:

Name Views Likes
Internship Opportunity at cppsecrets.com From Jan 2021 to Feb 2021 (For 2 months) 797 0
Internship Opportunity at cppsecrets From Jan 2010 to March 2010 (For 2 months) 142 0
Internship Opportunity at cppsecrets for Nov 2020 batch 4400 1
Internship Opportunity at cppsecrets for Sept 2020 batch 5725 1
Internship Opportunity at cppsecrets for July 2020 batch 14638 3
How to give paid exam at cppsecrets.com? 795 0
Internship Opportunity at cppsecrets for May 2020 batch 9606 4
How to give internship exam at cppsecrets.com 828 1
Internship Opportunity at cppsecrets for UPES Hackathon Participants 446 1
Internship Opportunity at cppsecrets for March 2020 batch 7259 4
C++ Boost Serialization: Serializing primitive data types 55 3
C++ challange for Oct 2019 Interns 4065 0
Python challange for Sept 2019 Interns 1974 5
Stipend update for the Python interns of June 2019 batch 913 3
C++ challange for Aug 2019 Interns 5170 4
ssh Bad owner or permissions 420 0
C++ Config Reader 2351 2
fatal error: Python.h: No such file or directory 744 0
Stipend update for the C++ interns of Feb 2019 1903 32
Python challange for May 2019 Interns 681 15
Stipend update for the Python interns of Feb 2019 856 17
C++ challange for Interns May 2019 1010 21
Python project to update a doc file conver it to the pdf file and send it to multiple email addresses 899 29
Dynamic Programming - Rod Cutting Problem 836 20
Dynamic Programming - Egg Dropping Problem 742 16
C program to get memory usage 2033 18
Python 2.7 Run linux command and get output 639 38
Python Command Line Arguments 549 11
Python Selected Interns (Feb 2019 Batch - 2) 665 21
Python Converting a String to a List of Words based on spaces and alphanumeric characters 523 25
Python Converting a String to a List of Words 404 19
C++ Selected Interns (Feb 2019 Batch - 2) 646 19
Python challange for Interns (Feb 2019, Batch 2) 482 13
Python Selected Interns (Feb 2019 Batch - 1) 664 24
C++ challange for Interns (Feb 2019, Batch 2) 524 24
C++ Selected Interns (Feb 2019 Batch - 1) 763 15
C++ challange for Feb 2019 Interns 668 11
GDB Print complete string 947 24
Python challange for Feb 2019 Interns 680 27
Python Top MOST famous Python libraries and framework 468 29
How to install Python Requests Library 363 13
Test article for interns Python Add Two Numbers 375 12
Python programs list 470 23
C++ Split string by delimiter and store it in vector 1322 12
Git create branch 432 21
Linux Extract an RPM package without installing it 469 16
Inorder Tree Traversal without recursion 512 14
Preorder Tree Traversal without recursion 392 16
Postorder Tree Traversal with recursion 431 21
Preorder Tree Traversal with recursion 600 12
Inorder Tree Traversal with recursion 431 25
Linux - find all the shell/terminals are running 476 29
Linux - Difference between tty and pts 3832 20
Linux Select Process By PPID 444 20
Linux Print Process Tree 442 27
C++ Is paragraph present in the file 409 22
C++ Two dimensional vector 478 21
C++ Range based for loop 409 18
Python UDP client server example code 1710 29
C++ Best way to trim std::string 1816 28
C++11 Auto Example Code 443 14
C++11 decltype and typeid 1023 16
C++ Abstract classes and pure virtual functions 436 23
Windows enable NTP Server 389 17
C++ Template with default arguments 411 20
C++ Object Slicing 330 14
C++ map of char* and char* 942 28
C++ Queue of objects 561 18
C++ Set of objects 394 21
Fork system call 397 15
Erase elements from vector 415 17
ps man page 320 19
Print all processes running as root 414 28
Select process by PPID 304 17
Difference between remove and remove_if in STL 441 19
UDP Client Program in C 827 23
UDP Server Program in C 4050 24
Linux Find out how many file descriptors are opened 329 14
Run unix command and get output 486 28
popen man page 296 15
OpenSSL generate hash of the file 566 22
CPU usage per thread without top utility 924 16
errno.h Error Codes in Linux 359 15
Compile C++11 program using g++ 857 10
Mutable lambda in C++ 1208 11
Valgrind complete tutorial 481 10
sudo error, is mode 0777, should be 0440 16 10
Check String is palindrome or not 567 10
Find N pairs with smallest sums in two vectors 243 11
Find the Nth largest element in a vector 792 11
Python List Built-In Methods 257 11
Python Sum of all the items in a list 251 11
C++ Compare version string 876 11
Python Tcp Server Example Code 402 12
C++ Check whether two strings are anagram of each other 1401 12
C++ Validate parenthesis pattern 313 10
Find maximum product of three numbers 239 11
C++ Count maximum number of consecutive ones in a vector 431 10
C++ Searching an element in a vector 259 11
C++ Sorting of vector 260 10
C++ Vector of objects 326 20
Template based linked list 758 11
Binary Tree Traversal 2096 21
C++ Generic file writer class 347 22
C++ Read and write binary file 708 22
C++ Read file and store it into integer variable 268 12
C++ Read file line by line 302 14
C++ File handling 468 11
C++ File read and write 291 12
C++ Abstract Class 280 21
C++ typeid operator 1830 21
C++11 decltype 231 12
C++11 auto keyword 311 21
C++11 Range based for loop 343 11
Nested Template Class 359 11
Ring Buffer 1553 11
C++ Parameter Binding 255 11
C++ Predicate 432 10
Singleton Class using C++ 11 635 12
Priority Queue 627 12
How to cin to vector 4052 10
How to cin to list 997 10
Overload Assignment Operator 254 10
Write copy constructor, assigment operator and desctructor of class having pointer variable 461 10
Deep Copy 315 11
Shallow Copy 853 10
Complex Number Program 253 10
Lambdas in C++ 337 11
Write your own shared pointer 321 10
Functor with arguments 593 10
Build-in Functors 244 10
Python send mail 484 22
C++ Pass local variable to Functor 304 10
Pass function pointer to a function 212 11
Functor in C++ 311 11
Function Pointer 303 10

Comments