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
C++ Abstract Class 391 21
Compile C++11 program using g++ 1502 10
C++ Searching an element in a vector 364 11
Priority Queue 996 12
Check String is palindrome or not 806 10
Stipend update for the Python interns of Feb 2019 983 17
Preorder Tree Traversal with recursion 750 12
Template based linked list 1858 11
Print all processes running as root 546 28
C++ Queue of objects 1482 18
How to cin to vector 8010 10
Stipend update for the Python interns of June 2019 batch 1079 3
Python List Built-In Methods 349 11
Python Basic Programs 267 1
C++ Compare version string 1468 11
C++ Vector of objects 861 20
Windows enable NTP Server 487 17
Tokenized securities on blockchain 326 0
Difference between remove and remove_if in STL 598 19
CPU usage per thread without top utility 1364 16
Python Selected Interns (Feb 2019 Batch - 1) 812 24
Complex Number Program 411 10
Functor with arguments 806 10
Pass function pointer to a function 278 11
C++ Two dimensional vector 575 21
C++ challange for Aug 2019 Interns 5467 4
C program to get memory usage 4398 18
Python challange for Interns (Feb 2019, Batch 2) 588 13
C++ Read file line by line 452 14
Dynamic Programming - Egg Dropping Problem 1135 16
GDB Print complete string 1315 24
UDP Server Program in C 4916 24
Internship Opportunity at cppsecrets From Jan 2010 to March 2010 (For 2 months) 329 0
C++ Split string by delimiter and store it in vector 1723 12
Python programs list 567 23
Mutable lambda in C++ 2042 11
C++ Sorting of vector 343 10
Overload Assignment Operator 336 10
Blockchain The Benefits and Risks of Smart Contracts 439 0
Inorder Tree Traversal without recursion 686 14
Lambdas in C++ 433 11
Inorder Tree Traversal with recursion 512 25
C++ Count maximum number of consecutive ones in a vector 734 10
C++ Parameter Binding 346 11
Python Converting a String to a List of Words 501 19
Internship Opportunity at cppsecrets.com From Mar 2021 to April 2021 (For 2 months) 3157 0
Python project to update a doc file conver it to the pdf file and send it to multiple email addresses 1297 29
Test article for interns Python Add Two Numbers 465 12
Ring Buffer 2880 11
Python 2.7 Run linux command and get output 833 38
C++ Predicate 671 10
Internship Opportunity at cppsecrets for Nov 2020 batch 4722 1
Find N pairs with smallest sums in two vectors 312 11
C++ Set of objects 505 21
C++ Selected Interns (Feb 2019 Batch - 1) 908 15
Internship Opportunity at cppsecrets.com From July 2021 to Aug 2021 (For 2 months) 3343 0
Python challange for Sept 2019 Interns 2176 5
Write your own shared pointer 448 10
Internship Opportunity at cppsecrets for May 2020 batch 10088 4
C++ Check whether two strings are anagram of each other 1877 12
C++11 Range based for loop 474 11
Erase elements from vector 893 17
Python send mail 596 22
Git create branch 504 21
ssh Bad owner or permissions 513 0
C++11 decltype 311 12
C++ Read file and store it into integer variable 370 12
Python Tcp Server Example Code 501 12
C++ challange for Oct 2019 Interns 4514 0
Linux - Difference between tty and pts 5906 20
sudo error, is mode 0777, should be 0440 16 10
C++ Config Reader 4160 3
Find maximum product of three numbers 310 11
Python Converting a String to a List of Words based on spaces and alphanumeric characters 652 25
Internship Opportunity at cppsecrets.com 4269 0
C++ map of char* and char* 1625 28
C++11 decltype and typeid 1401 16
Fork system call 501 15
Postorder Tree Traversal with recursion 537 21
How to give paid exam at cppsecrets.com? 932 0
Functor in C++ 405 11
Internship Opportunity at cppsecrets for UPES Hackathon Participants 529 1
C++11 auto keyword 423 21
C++ Boost Serialization: Serializing primitive data types 55 3
C++ Validate parenthesis pattern 377 10
Dynamic Programming - Rod Cutting Problem 1137 20
Linux Print Process Tree 519 27
Stipend update for the C++ interns of Feb 2019 2133 32
Linux Extract an RPM package without installing it 624 16
Preorder Tree Traversal without recursion 525 16
Internship Opportunity at cppsecrets for March 2020 batch 7571 4
Python challange for Feb 2019 Interns 801 27
UDP Client Program in C 1927 23
Deep Copy 477 11
Find the Nth largest element in a vector 1766 11
10 Most Frequent Laptop Issues and How to Solve them 115 0
Python Selected Interns (Feb 2019 Batch - 2) 772 21
C++ challange for Interns (Feb 2019, Batch 2) 625 24
Select process by PPID 375 17
C++ typeid operator 2384 21
Python UDP client server example code 2444 29
C++ Abstract classes and pure virtual functions 524 23
fatal error: Python.h: No such file or directory 977 0
Linux Find out how many file descriptors are opened 429 14
C++ Range based for loop 482 18
Binary Tree Traversal 2479 21
Python Command Line Arguments 700 11
How to install Python Requests Library 465 13
Valgrind complete tutorial 660 10
Function Pointer 387 10
C++ Is paragraph present in the file 483 22
How to cin to list 2179 10
Internship Opportunity at cppsecrets.com From May 2021 to June 2021 (For 2 months) 5698 0
C++11 Auto Example Code 539 14
Shallow Copy 1375 10
C++ File handling 738 11
C++ challange for Feb 2019 Interns 794 11
ps man page 405 19
errno.h Error Codes in Linux 467 15
Write copy constructor, assigment operator and desctructor of class having pointer variable 626 10
Internship Opportunity at cppsecrets.com From Jan 2021 to Feb 2021 (For 2 months) 4897 0
Internship Opportunity at cppsecrets for July 2020 batch 15379 4
popen man page 408 15
C++ Read and write binary file 1828 22
C++ Generic file writer class 570 22
C++ Object Slicing 418 14
OpenSSL generate hash of the file 892 22
Singleton Class using C++ 11 1364 12
C++ challange for Interns May 2019 1163 21
C++ Pass local variable to Functor 407 10
Nested Template Class 481 11
Python Sum of all the items in a list 329 11
How to give internship exam at cppsecrets.com 1834 1
C++ Selected Interns (Feb 2019 Batch - 2) 771 19
Run unix command and get output 1153 28
Python Top MOST famous Python libraries and framework 577 29
Linux - find all the shell/terminals are running 603 29
Python challange for May 2019 Interns 841 15
Linux Select Process By PPID 578 20
Internship Opportunity at cppsecrets for Sept 2020 batch 6029 1
C++ File read and write 375 12
C++ Best way to trim std::string 2760 28
Build-in Functors 325 10
C++ Template with default arguments 492 20
How To Move Forward After Losing Your Job Due To Covid 70 0

Comments