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 for July 2020 batch 3793 1
How to give paid exam at cppsecrets.com? 596 0
Internship Opportunity at cppsecrets for May 2020 batch 8936 4
How to give internship exam at cppsecrets.com 346 1
Internship Opportunity at cppsecrets for UPES Hackathon Participants 305 1
Internship Opportunity at cppsecrets for March 2020 batch 6765 3
C++ Boost Serialization: Serializing primitive data types 55 3
C++ challange for Oct 2019 Interns 3609 0
Python challange for Sept 2019 Interns 1726 5
Stipend update for the Python interns of June 2019 batch 685 3
C++ challange for Aug 2019 Interns 4720 4
ssh Bad owner or permissions 299 0
C++ Config Reader 1107 2
fatal error: Python.h: No such file or directory 492 0
Stipend update for the C++ interns of Feb 2019 1618 32
Python challange for May 2019 Interns 513 15
Stipend update for the Python interns of Feb 2019 712 17
C++ challange for Interns May 2019 782 21
Python project to update a doc file conver it to the pdf file and send it to multiple email addresses 691 29
Dynamic Programming - Rod Cutting Problem 573 19
Dynamic Programming - Egg Dropping Problem 542 16
C program to get memory usage 680 18
Python 2.7 Run linux command and get output 491 37
Python Command Line Arguments 416 11
Python Selected Interns (Feb 2019 Batch - 2) 507 21
Python Converting a String to a List of Words based on spaces and alphanumeric characters 410 25
Python Converting a String to a List of Words 297 19
C++ Selected Interns (Feb 2019 Batch - 2) 504 19
Python challange for Interns (Feb 2019, Batch 2) 359 13
Python Selected Interns (Feb 2019 Batch - 1) 516 24
C++ challange for Interns (Feb 2019, Batch 2) 382 24
C++ Selected Interns (Feb 2019 Batch - 1) 573 15
C++ challange for Feb 2019 Interns 518 11
GDB Print complete string 524 24
Python challange for Feb 2019 Interns 545 27
Python Top MOST famous Python libraries and framework 353 29
How to install Python Requests Library 265 13
Test article for interns Python Add Two Numbers 270 12
Python programs list 346 23
C++ Split string by delimiter and store it in vector 940 12
Git create branch 334 21
Linux Extract an RPM package without installing it 331 16
Inorder Tree Traversal without recursion 323 14
Preorder Tree Traversal without recursion 294 16
Postorder Tree Traversal with recursion 323 21
Preorder Tree Traversal with recursion 423 12
Inorder Tree Traversal with recursion 335 25
Linux - find all the shell/terminals are running 367 29
Linux - Difference between tty and pts 2500 20
Linux Select Process By PPID 339 20
Linux Print Process Tree 333 27
C++ Is paragraph present in the file 316 22
C++ Two dimensional vector 352 21
C++ Range based for loop 317 18
Python UDP client server example code 1091 29
C++ Best way to trim std::string 1201 28
C++11 Auto Example Code 333 14
C++11 decltype and typeid 672 16
C++ Abstract classes and pure virtual functions 354 23
Windows enable NTP Server 298 17
C++ Template with default arguments 306 20
C++ Object Slicing 239 14
C++ map of char* and char* 539 28
C++ Queue of objects 313 18
C++ Set of objects 332 21
Fork system call 331 15
Erase elements from vector 305 17
ps man page 255 19
Print all processes running as root 328 28
Select process by PPID 250 17
Difference between remove and remove_if in STL 311 19
UDP Client Program in C 524 23
UDP Server Program in C 3214 24
Linux Find out how many file descriptors are opened 258 14
Run unix command and get output 308 28
popen man page 211 15
OpenSSL generate hash of the file 370 22
CPU usage per thread without top utility 605 16
errno.h Error Codes in Linux 283 15
Compile C++11 program using g++ 521 10
Mutable lambda in C++ 857 11
Valgrind complete tutorial 356 10
sudo error, is mode 0777, should be 0440 16 10
Check String is palindrome or not 364 10
Find N pairs with smallest sums in two vectors 177 11
Find the Nth largest element in a vector 446 11
Python List Built-In Methods 198 11
Python Sum of all the items in a list 176 11
C++ Compare version string 516 11
Python Tcp Server Example Code 332 12
C++ Check whether two strings are anagram of each other 882 12
C++ Validate parenthesis pattern 243 10
Find maximum product of three numbers 185 11
C++ Count maximum number of consecutive ones in a vector 221 10
C++ Searching an element in a vector 186 11
C++ Sorting of vector 184 10
C++ Vector of objects 227 20
Template based linked list 272 11
Binary Tree Traversal 1747 21
C++ Generic file writer class 234 22
C++ Read and write binary file 357 22
C++ Read file and store it into integer variable 191 12
C++ Read file line by line 220 14
C++ File handling 269 11
C++ File read and write 216 12
C++ Abstract Class 215 21
C++ typeid operator 1355 21
C++11 decltype 180 12
C++11 auto keyword 237 21
C++11 Range based for loop 246 11
Nested Template Class 276 11
Ring Buffer 687 11
C++ Parameter Binding 196 11
C++ Predicate 276 10
Singleton Class using C++ 11 301 12
Priority Queue 374 12
How to cin to vector 1263 10
How to cin to list 418 10
Overload Assignment Operator 185 10
Write copy constructor, assigment operator and desctructor of class having pointer variable 296 10
Deep Copy 235 11
Shallow Copy 465 10
Complex Number Program 181 10
Lambdas in C++ 245 11
Write your own shared pointer 243 10
Functor with arguments 397 10
Build-in Functors 185 10
Python send mail 369 22
C++ Pass local variable to Functor 225 10
Pass function pointer to a function 163 11
Functor in C++ 223 11
Function Pointer 207 10

Comments