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 Sept 2020 batch 5493 1
Internship Opportunity at cppsecrets for July 2020 batch 14403 3
How to give paid exam at cppsecrets.com? 746 0
Internship Opportunity at cppsecrets for May 2020 batch 9443 4
How to give internship exam at cppsecrets.com 694 1
Internship Opportunity at cppsecrets for UPES Hackathon Participants 412 1
Internship Opportunity at cppsecrets for March 2020 batch 7107 4
C++ Boost Serialization: Serializing primitive data types 55 3
C++ challange for Oct 2019 Interns 3883 0
Python challange for Sept 2019 Interns 1893 5
Stipend update for the Python interns of June 2019 batch 853 3
C++ challange for Aug 2019 Interns 5019 4
ssh Bad owner or permissions 371 0
C++ Config Reader 1901 2
fatal error: Python.h: No such file or directory 675 0
Stipend update for the C++ interns of Feb 2019 1846 32
Python challange for May 2019 Interns 614 15
Stipend update for the Python interns of Feb 2019 799 17
C++ challange for Interns May 2019 940 21
Python project to update a doc file conver it to the pdf file and send it to multiple email addresses 836 29
Dynamic Programming - Rod Cutting Problem 742 19
Dynamic Programming - Egg Dropping Problem 680 16
C program to get memory usage 1355 18
Python 2.7 Run linux command and get output 582 38
Python Command Line Arguments 509 11
Python Selected Interns (Feb 2019 Batch - 2) 619 21
Python Converting a String to a List of Words based on spaces and alphanumeric characters 477 25
Python Converting a String to a List of Words 371 19
C++ Selected Interns (Feb 2019 Batch - 2) 595 19
Python challange for Interns (Feb 2019, Batch 2) 442 13
Python Selected Interns (Feb 2019 Batch - 1) 621 24
C++ challange for Interns (Feb 2019, Batch 2) 474 24
C++ Selected Interns (Feb 2019 Batch - 1) 712 15
C++ challange for Feb 2019 Interns 630 11
GDB Print complete string 771 24
Python challange for Feb 2019 Interns 637 27
Python Top MOST famous Python libraries and framework 430 29
How to install Python Requests Library 328 13
Test article for interns Python Add Two Numbers 342 12
Python programs list 438 23
C++ Split string by delimiter and store it in vector 1183 12
Git create branch 396 21
Linux Extract an RPM package without installing it 420 16
Inorder Tree Traversal without recursion 449 14
Preorder Tree Traversal without recursion 357 16
Postorder Tree Traversal with recursion 395 21
Preorder Tree Traversal with recursion 534 12
Inorder Tree Traversal with recursion 401 25
Linux - find all the shell/terminals are running 444 29
Linux - Difference between tty and pts 3297 20
Linux Select Process By PPID 408 20
Linux Print Process Tree 401 27
C++ Is paragraph present in the file 376 22
C++ Two dimensional vector 433 21
C++ Range based for loop 381 18
Python UDP client server example code 1481 29
C++ Best way to trim std::string 1563 28
C++11 Auto Example Code 416 14
C++11 decltype and typeid 918 16
C++ Abstract classes and pure virtual functions 412 23
Windows enable NTP Server 359 17
C++ Template with default arguments 374 20
C++ Object Slicing 304 14
C++ map of char* and char* 770 28
C++ Queue of objects 432 18
C++ Set of objects 368 21
Fork system call 367 15
Erase elements from vector 355 17
ps man page 295 19
Print all processes running as root 373 28
Select process by PPID 285 17
Difference between remove and remove_if in STL 394 19
UDP Client Program in C 678 23
UDP Server Program in C 3692 24
Linux Find out how many file descriptors are opened 304 14
Run unix command and get output 402 28
popen man page 261 15
OpenSSL generate hash of the file 484 22
CPU usage per thread without top utility 814 16
errno.h Error Codes in Linux 331 15
Compile C++11 program using g++ 733 10
Mutable lambda in C++ 1080 11
Valgrind complete tutorial 433 10
sudo error, is mode 0777, should be 0440 16 10
Check String is palindrome or not 509 10
Find N pairs with smallest sums in two vectors 217 11
Find the Nth largest element in a vector 650 11
Python List Built-In Methods 235 11
Python Sum of all the items in a list 223 11
C++ Compare version string 728 11
Python Tcp Server Example Code 375 12
C++ Check whether two strings are anagram of each other 1204 12
C++ Validate parenthesis pattern 281 10
Find maximum product of three numbers 220 11
C++ Count maximum number of consecutive ones in a vector 359 10
C++ Searching an element in a vector 229 11
C++ Sorting of vector 235 10
C++ Vector of objects 261 20
Template based linked list 417 11
Binary Tree Traversal 1959 21
C++ Generic file writer class 314 22
C++ Read and write binary file 534 22
C++ Read file and store it into integer variable 230 12
C++ Read file line by line 266 14
C++ File handling 380 11
C++ File read and write 264 12
C++ Abstract Class 256 21
C++ typeid operator 1672 21
C++11 decltype 210 12
C++11 auto keyword 276 21
C++11 Range based for loop 303 11
Nested Template Class 321 11
Ring Buffer 1155 11
C++ Parameter Binding 235 11
C++ Predicate 380 10
Singleton Class using C++ 11 522 12
Priority Queue 523 12
How to cin to vector 2667 10
How to cin to list 692 10
Overload Assignment Operator 226 10
Write copy constructor, assigment operator and desctructor of class having pointer variable 393 10
Deep Copy 284 11
Shallow Copy 668 10
Complex Number Program 222 10
Lambdas in C++ 297 11
Write your own shared pointer 292 10
Functor with arguments 526 10
Build-in Functors 223 10
Python send mail 445 22
C++ Pass local variable to Functor 276 10
Pass function pointer to a function 189 11
Functor in C++ 278 11
Function Pointer 273 10

Comments