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 2228 0
Internship Opportunity at cppsecrets.com From May 2021 to June 2021 (For 2 months) 3515 0
Tokenized securities on blockchain 226 0
Blockchain The Benefits and Risks of Smart Contracts 377 0
Internship Opportunity at cppsecrets.com From Mar 2021 to April 2021 (For 2 months) 2879 0
Python Basic Programs 179 1
Internship Opportunity at cppsecrets.com From Jan 2021 to Feb 2021 (For 2 months) 4674 0
Internship Opportunity at cppsecrets From Jan 2010 to March 2010 (For 2 months) 276 0
Internship Opportunity at cppsecrets for Nov 2020 batch 4626 1
Internship Opportunity at cppsecrets for Sept 2020 batch 5907 1
Internship Opportunity at cppsecrets for July 2020 batch 15023 3
How to give paid exam at cppsecrets.com? 869 0
Internship Opportunity at cppsecrets for May 2020 batch 9854 4
How to give internship exam at cppsecrets.com 1106 1
Internship Opportunity at cppsecrets for UPES Hackathon Participants 501 1
Internship Opportunity at cppsecrets for March 2020 batch 7450 4
C++ Boost Serialization: Serializing primitive data types 55 3
C++ challange for Oct 2019 Interns 4334 0
Python challange for Sept 2019 Interns 2084 5
Stipend update for the Python interns of June 2019 batch 994 3
C++ challange for Aug 2019 Interns 5328 4
ssh Bad owner or permissions 472 0
C++ Config Reader 3414 3
fatal error: Python.h: No such file or directory 840 0
Stipend update for the C++ interns of Feb 2019 2047 32
Python challange for May 2019 Interns 742 15
Stipend update for the Python interns of Feb 2019 915 17
C++ challange for Interns May 2019 1092 21
Python project to update a doc file conver it to the pdf file and send it to multiple email addresses 1147 29
Dynamic Programming - Rod Cutting Problem 1044 20
Dynamic Programming - Egg Dropping Problem 1045 16
C program to get memory usage 3418 18
Python 2.7 Run linux command and get output 727 38
Python Command Line Arguments 646 11
Python Selected Interns (Feb 2019 Batch - 2) 733 21
Python Converting a String to a List of Words based on spaces and alphanumeric characters 592 25
Python Converting a String to a List of Words 462 19
C++ Selected Interns (Feb 2019 Batch - 2) 713 19
Python challange for Interns (Feb 2019, Batch 2) 546 13
Python Selected Interns (Feb 2019 Batch - 1) 741 24
C++ challange for Interns (Feb 2019, Batch 2) 569 24
C++ Selected Interns (Feb 2019 Batch - 1) 840 15
C++ challange for Feb 2019 Interns 745 11
GDB Print complete string 1134 24
Python challange for Feb 2019 Interns 746 27
Python Top MOST famous Python libraries and framework 526 29
How to install Python Requests Library 423 13
Test article for interns Python Add Two Numbers 423 12
Python programs list 520 23
C++ Split string by delimiter and store it in vector 1563 12
Git create branch 468 21
Linux Extract an RPM package without installing it 552 16
Inorder Tree Traversal without recursion 622 14
Preorder Tree Traversal without recursion 453 16
Postorder Tree Traversal with recursion 487 21
Preorder Tree Traversal with recursion 685 12
Inorder Tree Traversal with recursion 476 25
Linux - find all the shell/terminals are running 547 29
Linux - Difference between tty and pts 4946 20
Linux Select Process By PPID 520 20
Linux Print Process Tree 483 27
C++ Is paragraph present in the file 453 22
C++ Two dimensional vector 528 21
C++ Range based for loop 452 18
Python UDP client server example code 2107 29
C++ Best way to trim std::string 2316 28
C++11 Auto Example Code 494 14
C++11 decltype and typeid 1233 16
C++ Abstract classes and pure virtual functions 478 23
Windows enable NTP Server 439 17
C++ Template with default arguments 458 20
C++ Object Slicing 377 14
C++ map of char* and char* 1268 28
C++ Queue of objects 1016 18
C++ Set of objects 454 21
Fork system call 450 15
Erase elements from vector 680 17
ps man page 373 19
Print all processes running as root 492 28
Select process by PPID 347 17
Difference between remove and remove_if in STL 522 19
UDP Client Program in C 1298 23
UDP Server Program in C 4515 24
Linux Find out how many file descriptors are opened 386 14
Run unix command and get output 793 28
popen man page 348 15
OpenSSL generate hash of the file 754 22
CPU usage per thread without top utility 1145 16
errno.h Error Codes in Linux 416 15
Compile C++11 program using g++ 1141 10
Mutable lambda in C++ 1589 11
Valgrind complete tutorial 583 10
sudo error, is mode 0777, should be 0440 16 10
Check String is palindrome or not 699 10
Find N pairs with smallest sums in two vectors 284 11
Find the Nth largest element in a vector 1285 11
Python List Built-In Methods 292 11
Python Sum of all the items in a list 295 11
C++ Compare version string 1189 11
Python Tcp Server Example Code 464 12
C++ Check whether two strings are anagram of each other 1636 12
C++ Validate parenthesis pattern 352 10
Find maximum product of three numbers 279 11
C++ Count maximum number of consecutive ones in a vector 575 10
C++ Searching an element in a vector 304 11
C++ Sorting of vector 297 10
C++ Vector of objects 523 20
Template based linked list 1391 11
Binary Tree Traversal 2384 21
C++ Generic file writer class 470 22
C++ Read and write binary file 1217 22
C++ Read file and store it into integer variable 335 12
C++ Read file line by line 385 14
C++ File handling 636 11
C++ File read and write 339 12
C++ Abstract Class 327 21
C++ typeid operator 2101 21
C++11 decltype 271 12
C++11 auto keyword 373 21
C++11 Range based for loop 424 11
Nested Template Class 420 11
Ring Buffer 2329 11
C++ Parameter Binding 299 11
C++ Predicate 584 10
Singleton Class using C++ 11 1016 12
Priority Queue 807 12
How to cin to vector 6532 10
How to cin to list 1664 10
Overload Assignment Operator 301 10
Write copy constructor, assigment operator and desctructor of class having pointer variable 558 10
Deep Copy 372 11
Shallow Copy 1162 10
Complex Number Program 333 10
Lambdas in C++ 386 11
Write your own shared pointer 389 10
Functor with arguments 702 10
Build-in Functors 294 10
Python send mail 546 22
C++ Pass local variable to Functor 356 10
Pass function pointer to a function 241 11
Functor in C++ 360 11
Function Pointer 352 10

Comments