1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
|
<!DOCTYPE html>
<html lang="en" dir="ltr" class="client-nojs">
<head>
<title>Lambda functions (since C++11) - cppreference.com</title>
<meta charset="UTF-8" />
<meta name="generator" content="MediaWiki 1.21.2" />
<link rel="alternate" type="application/x-wiki" title="Edit" href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=edit" />
<link rel="edit" title="Edit" href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=edit" />
<link rel="shortcut icon" href="../../../favicon.ico" />
<link rel="search" type="application/opensearchdescription+xml" href="../../../mwiki/opensearch_desc.php" title="cppreference.com (en)" />
<link rel="EditURI" type="application/rsd+xml" href="http://en.cppreference.com/mwiki/api.php?action=rsd" />
<link rel="alternate" type="application/atom+xml" title="cppreference.com Atom feed" href="http://en.cppreference.com/mwiki/index.php?title=Special:RecentChanges&feed=atom" />
<link rel="stylesheet" href="../../../mwiki/load.php%3Fdebug=false&lang=en&modules=ext.gadget.ColiruCompiler%257Cext.rtlcite%257Cmediawiki.legacy.commonPrint%252Cshared%257Cskins.cppreference2&only=styles&skin=cppreference2&*.css" />
<meta name="ResourceLoaderDynamicStyles" content="" />
<link rel="stylesheet" href="../../../mwiki/load.php%3Fdebug=false&lang=en&modules=site&only=styles&skin=cppreference2&*.css" />
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_en_:resourceloader:filter:minify-css:7:472787eddcf4605d11de8c7ef047234f */</style>
<script src="../../../mwiki/load.php%3Fdebug=false&lang=en&modules=startup&only=scripts&skin=cppreference2&*"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/lambda","wgTitle":"cpp/language/lambda","wgCurRevisionId":81678,"wgArticleId":5059,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"en","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgMonthNamesShort":["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"wgRelevantPageName":"cpp/language/lambda","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"en","language":"en","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_en_:resourceloader:filter:minify-js:7:ca03345b1e2c4d90a25d968753a73b92 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
line-height: normal; border: 0px none white;
}
/**
* GeSHi Dynamically Generated Stylesheet
* --------------------------------------
* Dynamically generated stylesheet for cpp
* CSS class: source-cpp, CSS id:
* GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
* (http://qbnz.com/highlighter/ and http://geshi.org/)
* --------------------------------------
*/
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }
/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
line-height: normal; border: 0px none white;
}
/**
* GeSHi Dynamically Generated Stylesheet
* --------------------------------------
* Dynamically generated stylesheet for text
* CSS class: source-text, CSS id:
* GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
* (http://qbnz.com/highlighter/ and http://geshi.org/)
* --------------------------------------
*/
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }
/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_lambda skin-cppreference2 action-view cpp-navbar">
<!-- header -->
<div id="mw-head" class="noprint">
<div id="cpp-head-first-base">
<div id="cpp-head-first">
<h5><a href="../../../index.html">
cppreference.com </a></h5>
<div id="cpp-head-search">
<!-- 0 -->
<div id="p-search">
<h5><label for="searchInput">Search</label></h5>
<form action="http://en.cppreference.com/mwiki/index.php" id="searchform">
<input type='hidden' name="title" value="Special:Search"/>
<div id="simpleSearch">
<input name="search" title="Search cppreference.com [f]" accesskey="f" id="searchInput" /> <button type="submit" name="button" title="Search the pages for this text" id="searchButton"><img src="../../../mwiki/skins/cppreference2/images/search-ltr.png%3F303" alt="Search" /></button> </div>
</form>
</div>
<!-- /0 -->
</div>
<div id="cpp-head-personal">
<!-- 0 -->
<div id="p-personal" class="">
<span id="pt-createaccount"><a href="http://en.cppreference.com/mwiki/index.php?title=Special:UserLogin&returnto=cpp%2Flanguage%2Flambda&type=signup">Create account</a></span> <div class="menu">
<ul>
<li id="pt-login"><a href="http://en.cppreference.com/mwiki/index.php?title=Special:UserLogin&returnto=cpp%2Flanguage%2Flambda" title="You are encouraged to log in; however, it is not mandatory [o]" accesskey="o">Log in</a></li> </ul>
</div>
</div>
<!-- /0 -->
</div>
</div>
</div>
<div id="cpp-head-second-base">
<div id="cpp-head-second">
<div id="cpp-head-tools-left">
<!-- 0 -->
<div id="p-namespaces" class="vectorTabs">
<h5>Namespaces</h5>
<ul>
<li id="ca-nstab-main" class="selected"><span><a href="lambda.html" title="View the content page [c]" accesskey="c">Page</a></span></li>
<li id="ca-talk"><span><a href="http://en.cppreference.com/w/Talk:cpp/language/lambda" title="Discussion about the content page [t]" accesskey="t">Discussion</a></span></li>
</ul>
</div>
<!-- /0 -->
<!-- 1 -->
<div id="p-variants" class="vectorMenu emptyPortlet">
<h5><span>Variants</span><a href="lambda.html#"></a></h5>
<div class="menu">
<ul>
</ul>
</div>
</div>
<!-- /1 -->
</div>
<div id="cpp-head-tools-right">
<!-- 0 -->
<div id="p-views" class="vectorTabs">
<h5>Views</h5>
<ul>
<li id="ca-view" class="selected"><span><a href="lambda.html" >View</a></span></li>
<li id="ca-edit"><span><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=edit" title="You can edit this page. Please use the preview button before saving [e]" accesskey="e">Edit</a></span></li>
<li id="ca-history" class="collapsible"><span><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=history" title="Past revisions of this page [h]" accesskey="h">History</a></span></li>
</ul>
</div>
<!-- /0 -->
<!-- 1 -->
<div id="p-cactions" class="vectorMenu emptyPortlet">
<h5><span>Actions</span><a href="lambda.html#"></a></h5>
<div class="menu">
<ul>
</ul>
</div>
</div>
<!-- /1 -->
</div>
</div>
</div>
</div>
<!-- /header -->
<!-- content -->
<div id="cpp-content-base">
<div id="content">
<a id="top"></a>
<div id="mw-js-message" style="display:none;"></div>
<!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">Lambda functions <span class="t-mark-rev t-since-cxx11">(since C++11)</span></h1>
<!-- /firstHeading -->
<!-- bodyContent -->
<div id="bodyContent">
<!-- tagline -->
<div id="siteSub">From cppreference.com</div>
<!-- /tagline -->
<!-- subtitle -->
<div id="contentSub"><span class="subpages">< <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.1.html" title="cpp/language">language</a></span></div>
<!-- /subtitle -->
<!-- bodycontent -->
<div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.1.html" title="cpp/language"> Language</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header"> Standard library headers</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concept.html" title="cpp/concept"> Concepts</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility"> Utilities library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string"> Strings library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container"> Containers library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm"> Algorithms library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator"> Iterators library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric"> Numerics library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io"> Input/output library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale"> Localizations library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex"> Regular expressions library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic"> Atomic operations library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread"> Thread support library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental"> Technical Specifications</a> </td></tr>
</table></div><div><span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/navbar_content&action=edit">[edit]</a></span></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.1.html" title="cpp/language"> C++ language</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="functions.html" title="cpp/language/functions"> Functions</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> Declarations </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> function declaration</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html#Parameter_list" title="cpp/language/function"> function parameter list</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html#Function_definition" title="cpp/language/function"> function definition</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="default_arguments.html" title="cpp/language/default arguments"> default arguments</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="variadic_arguments.html" title="cpp/language/variadic arguments"> variadic arguments</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="inline.html" title="cpp/language/inline"> inline specifier</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> lambda expression</strong> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Function calls </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="adl.html" title="cpp/language/adl"> argument-dependent lookup (ADL)</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_other.html#Built-in_function_call_operator" title="cpp/language/operator other"> function-call operator</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concept/FunctionObject.html" title="cpp/concept/FunctionObject"> function objects</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> Overloading </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="overload_resolution.html" title="cpp/language/overload resolution"> overload resolution</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operators.html" title="cpp/language/operators"> operator overloading</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="overloaded_address.html" title="cpp/language/overloaded address"> address of an overload set</a> </td></tr>
</table></div>
</div><div><span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/language/functions/navbar_content&action=edit">[edit]</a></span></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>Constructs a <a href="http://en.wikipedia.com/wiki/Closure_(computer_science)" class="extiw" title="enwiki:Closure (computer science)">closure</a>: an unnamed function object capable of capturing variables in scope.
</p>
<table id="toc" class="toc"><tr><td><div id="toctitle"><h2>Contents</h2></div>
<ul>
<li class="toclevel-1 tocsection-1"><a href="lambda.html#Syntax"><span class="tocnumber">1</span> <span class="toctext">Syntax</span></a></li>
<li class="toclevel-1 tocsection-2"><a href="lambda.html#Explanation"><span class="tocnumber">2</span> <span class="toctext">Explanation</span></a></li>
<li class="toclevel-1"><a href="lambda.html#ClosureType::operator.28.29.28params.29"><span class="tocnumber">3</span> <span class="toctext"><span>ClosureType::</span>operator()(<span>params</span>)</span></a>
<ul>
<li class="toclevel-2"><a href="lambda.html#Dangling_references"><span class="tocnumber">3.1</span> <span class="toctext">Dangling references</span></a></li>
</ul>
</li>
<li class="toclevel-1"><a href="lambda.html#ClosureType::operator_ret.28.2A.29.28params.29.28.29"><span class="tocnumber">4</span> <span class="toctext"><span>ClosureType::</span>operator ret(*)(<span>params</span>)()</span></a></li>
<li class="toclevel-1"><a href="lambda.html#ClosureType::ClosureType.28.29"><span class="tocnumber">5</span> <span class="toctext"><span>ClosureType::</span>ClosureType()</span></a></li>
<li class="toclevel-1"><a href="lambda.html#ClosureType::operator.3D.28const_ClosureType.26.29"><span class="tocnumber">6</span> <span class="toctext"><span>ClosureType::</span>operator=(const ClosureType&)</span></a></li>
<li class="toclevel-1"><a href="lambda.html#ClosureType::.7EClosureType.28.29"><span class="tocnumber">7</span> <span class="toctext"><span>ClosureType::</span>~ClosureType()</span></a></li>
<li class="toclevel-1"><a href="lambda.html#ClosureType::Captures"><span class="tocnumber">8</span> <span class="toctext"><span>ClosureType::</span><span>Captures</span></span></a>
<ul>
<li class="toclevel-2 tocsection-4"><a href="lambda.html#Lambda_capture"><span class="tocnumber">8.1</span> <span class="toctext">Lambda capture</span></a></li>
<li class="toclevel-2 tocsection-5"><a href="lambda.html#Example"><span class="tocnumber">8.2</span> <span class="toctext">Example</span></a></li>
<li class="toclevel-2 tocsection-6"><a href="lambda.html#Defect_reports"><span class="tocnumber">8.3</span> <span class="toctext">Defect reports</span></a></li>
<li class="toclevel-2 tocsection-7"><a href="lambda.html#See_also"><span class="tocnumber">8.4</span> <span class="toctext">See also</span></a></li>
</ul>
</li>
</ul>
</td></tr></table>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=edit&section=1" title="Edit section: Syntax">edit</a>]</span> <span class="mw-headline" id="Syntax">Syntax</span></h3>
<table class="t-sdsc-begin">
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>[</b></code> <span class="t-spar">capture-list</span> <code><b>]</b></code> <code><b>(</b></code> <span class="t-spar">params</span> <code><b>)</b></code> <code><b>mutable</b></code><span class="t-mark">(optional)</span> <span class="t-spar">exception</span> <span class="t-spar">attribute</span> <code><b>-></b></code> <span class="t-spar">ret</span> <code><b>{</b></code> <span class="t-spar">body</span> <code><b>}</b></code>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>[</b></code> <span class="t-spar">capture-list</span> <code><b>]</b></code> <code><b>(</b></code> <span class="t-spar">params</span> <code><b>)</b></code> <code><b>-></b></code> <span class="t-spar">ret</span> <code><b>{</b></code> <span class="t-spar">body</span> <code><b>}</b></code>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>[</b></code> <span class="t-spar">capture-list</span> <code><b>]</b></code> <code><b>(</b></code> <span class="t-spar">params</span> <code><b>)</b></code> <code><b>{</b></code> <span class="t-spar">body</span> <code><b>}</b></code>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>[</b></code> <span class="t-spar">capture-list</span> <code><b>]</b></code> <code><b>{</b></code> <span class="t-spar">body</span> <code><b>}</b></code>
</td>
<td> (4)
</td>
<td class="t-sdsc-nopad">
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>1) Full declaration.
</p><p>2) Declaration of a const lambda: the objects captured by copy cannot be modified.
</p><p>3) Omitted trailing-return-type: the return type of the closure's <code>operator()</code> is determined according to the following rules:
</p>
<table class="t-rev-begin">
<tr class="t-rev t-until-cxx14"><td>
<ul><li> if the <span class="t-spar">body</span> consists of nothing but a single <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">return</span></span></span> statement with an expression, the return type is the type of the returned expression (after <a href="implicit_cast.html#Lvalue_transformations" title="cpp/language/implicit cast">lvalue-to-rvalue, array-to-pointer, or function-to-pointer implicit conversion</a>);
</li><li> otherwise, the return type is <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">void</span></span></span>.
</li></ul>
</td>
<td><span class="t-mark-rev t-until-cxx14">(until C++14)</span></td></tr><tr class="t-rev t-since-cxx14"><td>
<p>The return type is <a href="template_argument_deduction.html" title="cpp/language/template argument deduction">deduced</a> from <a href="return.html" title="cpp/language/return"><tt>return</tt></a> statements as if for a function whose <a href="function.html#Return_type_deduction" title="cpp/language/function">return type is declared auto</a>.
</p>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<p>4) Omitted parameter list: function takes no arguments, as if the parameter list was <code><b>()</b></code>.
</p>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=edit&section=2" title="Edit section: Explanation">edit</a>]</span> <span class="mw-headline" id="Explanation">Explanation</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td> mutable
</td>
<td> -
</td>
<td> allows <span class="t-spar">body</span> to modify the parameters captured by copy, and to call their non-const member functions
</td></tr>
<tr class="t-par">
<td> <span class="t-spar">exception</span>
</td>
<td> -
</td>
<td> provides the <a href="except_spec.html" title="cpp/language/except spec">exception specification</a> or the <a href="noexcept_spec.html" title="cpp/language/noexcept spec">noexcept clause</a> for operator() of the closure type
</td></tr>
<tr class="t-par">
<td> <span class="t-spar">attribute</span>
</td>
<td> -
</td>
<td> provides the <a href="attributes.html" title="cpp/language/attributes">attribute specification</a> for operator() of the closure type
</td></tr>
<tr class="t-par">
<td> <span class="t-spar">capture-list</span>
</td>
<td> -
</td>
<td> a comma-separated list of zero or more <a href="lambda.html#Lambda_capture">captures</a>, optionally beginning with a <span class="t-spar">capture-default</span>.
<p>Capture list can be passed as follows (see below for the detailed description):
</p>
<ul><li><code><b>[a,&b]</b></code> where <i>a</i> is captured by value and <i>b</i> is captured by reference.
</li><li><code><b>[this]</b></code> captures the <a href="this.html" title="cpp/language/this"><code><b>this</b></code> pointer</a> by value
</li><li><code><b>[&]</b></code> captures all <a href="storage_duration.html" title="cpp/language/storage duration">automatic</a> variables <a href="definition.html#ODR-use" title="cpp/language/definition">odr-used</a> in the body of the lambda by reference
</li><li><code><b>[=]</b></code> captures all <a href="storage_duration.html" title="cpp/language/storage duration">automatic</a> variables <a href="definition.html#ODR-use" title="cpp/language/definition">odr-used</a> in the body of the lambda by value
</li><li><code><b>[]</b></code> captures nothing
</li></ul>
</td></tr>
<tr class="t-par">
<td> <span class="t-spar">params</span>
</td>
<td> -
</td>
<td> The list of parameters, as in <a href="function.html" title="cpp/language/function">named functions</a><span class="t-rev-inl t-until-cxx14"><span>, except that <a href="default_arguments.html" title="cpp/language/default arguments">default arguments</a> are not allowed</span> <span><span class="t-mark-rev t-until-cxx14">(until C++14)</span></span></span>. <span class="t-rev-inl t-since-cxx14"><span>if <code>auto</code> is used as a type of a parameter, the lambda is a <i>generic lambda</i></span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span>
</td></tr>
<tr class="t-par">
<td> <span class="t-spar">ret</span>
</td>
<td> -
</td>
<td> Return type. If not present it's implied by the function return statements ( or void if it doesn't return any value)
</td></tr>
<tr class="t-par">
<td> <span class="t-spar">body</span>
</td>
<td> -
</td>
<td> Function body
</td></tr></table>
<p>The lambda expression constructs an unnamed prvalue temporary object of unique unnamed non-union non-aggregate type, known as <i>closure type</i>, which is declared (for the purposes of <a href="adl.html" title="cpp/language/adl">ADL</a>) in the smallest block scope, class scope, or namespace scope that contains the lambda expression. The closure type has the following members:
</p>
<div class="t-member">
<h2> <span class="mw-headline" id="ClosureType::operator.28.29.28params.29"> <span style="font-size:0.7em; line-height:130%">ClosureType::</span>operator()(<span class="t-spar">params</span>) </span></h2>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td> <div><span class="mw-geshi cpp source-cpp">ret operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span>params<span class="br0">)</span> <span class="kw4">const</span> <span class="br0">{</span> body <span class="br0">}</span></span></div></td>
<td class="t-dcl-nopad"> </td>
<td> <span class="t-mark">(the keyword mutable was not used)</span> </td>
</tr>
<tr class="t-dcl">
<td> <div><span class="mw-geshi cpp source-cpp">ret operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span>params<span class="br0">)</span> <span class="br0">{</span> body <span class="br0">}</span></span></div></td>
<td class="t-dcl-nopad"> </td>
<td> <span class="t-mark">(the keyword mutable was used)</span> </td>
</tr>
<tr class="t-dcl t-since-cxx14">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1"><</span>template<span class="sy2">-</span>params<span class="sy1">></span><br />
ret operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span>params<span class="br0">)</span> <span class="br0">{</span> body <span class="br0">}</span></span></div></td>
<td class="t-dcl-nopad"> </td>
<td> <span class="t-mark-rev t-since-cxx14">(since C++14)</span> <br /><span class="t-mark">(generic lambda)</span> </td>
</tr>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>Executes the body of the lambda-expression, when invoked. When accessing a variable, accesses its captured copy (for the entities captured by copy), or the original object (for the entities captured by reference). Unless the keyword <span class="t-c"><span class="mw-geshi cpp source-cpp">mutable</span></span> was used in the lambda-expression, the function-call operator is const-qualified and the objects that were captured by copy are non-modifiable from inside this <span class="t-c"><span class="mw-geshi cpp source-cpp">operator<span class="br0">(</span><span class="br0">)</span></span></span>. The function-call operator is never volatile-qualified and never virtual.
</p>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<p>For every parameter in <span class="t-spar">params</span> whose type is specified as <code>auto</code>, an invented template parameter is added to <span class="t-spar">template-params</span>, in order of appearance. The invented template parameter may be a <a href="parameter_pack.html" title="cpp/language/parameter pack">parameter pack</a> if the corresponding function member of <span class="t-spar">params</span> is a function parameter pack.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// generic lambda, operator() is a template with two parameters</span>
<span class="kw4">auto</span> glambda <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> a, <span class="kw4">auto</span><span class="sy3">&&</span> b<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> a <span class="sy1"><</span> b<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">bool</span> b <span class="sy1">=</span> glambda<span class="br0">(</span><span class="nu0">3</span>, <span class="nu16">3.14</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// ok</span>
 
<span class="co1">// generic lambda, operator() is a template with one parameter</span>
<span class="kw4">auto</span> vglambda <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> printer<span class="br0">)</span> <span class="br0">{</span>
<span class="kw1">return</span> <span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span><span class="sy3">&&</span>... <span class="me1">ts</span><span class="br0">)</span> <span class="co1">// generic lambda, ts is a parameter pack</span>
<span class="br0">{</span>
printer<span class="br0">(</span><a href="../utility/forward.html"><span class="kw947">std::<span class="me2">forward</span></span></a><span class="sy1"><</span>decltype<span class="br0">(</span>ts<span class="br0">)</span><span class="sy1">></span><span class="br0">(</span>ts<span class="br0">)</span>...<span class="br0">)</span><span class="sy4">;</span>
<span class="kw1">return</span> <span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span> <span class="br0">{</span> printer<span class="br0">(</span>ts...<span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// nullary lambda (takes no parameters)</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">auto</span> p <span class="sy1">=</span> vglambda<span class="br0">(</span><span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> v1, <span class="kw4">auto</span> v2, <span class="kw4">auto</span> v3<span class="br0">)</span> <span class="br0">{</span> <a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> v1 <span class="sy1"><<</span> v2 <span class="sy1"><<</span> v3<span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">auto</span> q <span class="sy1">=</span> p<span class="br0">(</span><span class="nu0">1</span>, <span class="st0">'a'</span>, <span class="nu16">3.14</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// outputs 1a3.14</span>
q<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// outputs 1a3.14</span></pre></div></div>
<p><code>ClosureType</code>'s <code>operator()</code> cannot be explicitly instantiated or explicitly specialized.
</p>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<p>the exception specification <span class="t-spar">exception</span> on the lambda-expression applies to the function-call operator or operator template.
</p><p>For the purpose of <a href="lookup.html" title="cpp/language/lookup">name lookup</a>, determining the type and value of the <a href="this.html" title="cpp/language/this">this pointer</a> and for accessing non-static class members, the body of the closure type's function call operator is considered in the context of the lambda-expression.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> X <span class="br0">{</span>
<span class="kw4">int</span> x, y<span class="sy4">;</span>
<span class="kw4">int</span> operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="co1">// the context of the following lambda is the member function X::f</span>
<span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span><span class="br0">(</span><span class="br0">)</span><span class="sy2">-</span><span class="sy1">></span><span class="kw4">int</span>
<span class="br0">{</span>
<span class="kw1">return</span> operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span>this<span class="sy2">-</span><span class="sy1">></span>x <span class="sy2">+</span> y<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// X::operator()(this->x + (*this).y)</span>
<span class="co1">// this has type X*</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<p><code>ClosureType</code>'s <code>operator()</code> cannot be named in a <a href="friend.html" title="cpp/language/friend">friend</a> declaration.
</p>
<h3> <span class="mw-headline" id="Dangling_references">Dangling references</span></h3>
<p>If an entity is captured by reference, implicitly or explicitly, and the function call operator of the closure object is invoked after the entity's lifetime has ended, undefined behavior occurs. The C++ closures do not extend the lifetimes of the captured references.
</p><p>Same applies to the lifetime of the object pointed to by the captured <code>this</code> pointer.
</p>
</div>
<div class="t-member">
<h2> <span class="mw-headline" id="ClosureType::operator_ret.28.2A.29.28params.29.28.29"> <span style="font-size:0.7em; line-height:130%">ClosureType::</span>operator ret(*)(<span class="t-spar">params</span>)() </span></h2>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">typedef</span> ret<span class="br0">(</span><span class="sy2">*</span>F<span class="br0">)</span><span class="br0">(</span>params<span class="br0">)</span><span class="sy4">;</span><br />
operator F<span class="br0">(</span><span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad"> </td>
<td> <span class="t-mark">(capture-less non-generic lambda)</span> </td>
</tr>
<tr class="t-dcl t-since-cxx14">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1"><</span>template<span class="sy2">-</span>params<span class="sy1">></span> <span class="kw1">using</span> fptr_t <span class="sy1">=</span> <span class="coMULTI">/*see below*/</span><span class="sy4">;</span><br />
<p><span class="kw1">template</span><span class="sy1"><</span>template<span class="sy2">-</span>params<span class="sy1">></span><br />
</p>
operator fptr_t<span class="sy1"><</span>template<span class="sy2">-</span>params<span class="sy1">></span><span class="br0">(</span><span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad"> </td>
<td> <span class="t-mark-rev t-since-cxx14">(since C++14)</span> <br /><span class="t-mark">(capture-less generic lambda)</span> </td>
</tr>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>This <a href="cast_operator.html" title="cpp/language/cast operator">user-defined conversion function</a> is only defined if the capture list of the lambda-expression is empty. It is a public, non-virtual, non-explicit, const <span class="t-rev-inl t-since-cxx14"><span>noexcept</span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span> member function of the closure object.
</p>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<p>A generic captureless lambda has user-defined conversion function template with the same invented template parameter list as the function-call operator template. If the return type is empty or auto, it is obtained by return type deduction on the function template specialization, which, in turn, is obtained by <a href="template_argument_deduction.html" title="cpp/language/template argument deduction">template argument deduction</a> for conversion function templates.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f1<span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
<span class="kw4">void</span> f2<span class="br0">(</span><span class="kw4">char</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
<span class="kw4">void</span> h<span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span> <span class="co1">// #1</span>
<span class="kw4">void</span> h<span class="br0">(</span><span class="kw4">char</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span> <span class="co1">// #2</span>
<span class="kw4">auto</span> glambda <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> a<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> a<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
f1<span class="br0">(</span>glambda<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// ok</span>
f2<span class="br0">(</span>glambda<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// error: not convertible</span>
h<span class="br0">(</span>glambda<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// ok: calls #1 since #2 is not convertible</span>
 
<span class="kw4">int</span><span class="sy3">&</span> <span class="br0">(</span><span class="sy2">*</span>fpi<span class="br0">)</span><span class="br0">(</span><span class="kw4">int</span><span class="sy2">*</span><span class="br0">)</span> <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span><span class="sy2">*</span> a<span class="br0">)</span><span class="sy2">-</span><span class="sy1">></span><span class="kw4">auto</span><span class="sy3">&</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="sy2">*</span>a<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// ok</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<p>The value returned by this conversion function is a pointer to a function with C++ <a href="language_linkage.html" title="cpp/language/language linkage">language linkage</a> that, when invoked, has the same effect as invoking the closure object's function call operator directly.
</p>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<p>If the closure object's <code>operator()</code> has a non-throwing exception specification, then the pointer returned by this function has the type pointer to noexcept function.
</p>
</td>
<td><span class="t-mark-rev t-since-cxx17">(since C++17)</span></td></tr>
</table>
</div>
<div class="t-member">
<h2> <span class="mw-headline" id="ClosureType::ClosureType.28.29"> <span style="font-size:0.7em; line-height:130%">ClosureType::</span>ClosureType() </span></h2>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td> <div><span class="mw-geshi cpp source-cpp">ClosureType<span class="br0">(</span><span class="br0">)</span> <span class="sy1">=</span> delete<span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad"> </td>
<td> <span class="t-mark-rev t-until-cxx14">(until C++14)</span> </td>
</tr>
<tr class="t-dcl">
<td> <div><span class="mw-geshi cpp source-cpp">ClosureType<span class="br0">(</span><span class="kw4">const</span> ClosureType<span class="sy3">&</span> <span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad"> </td>
<td> <span class="t-mark-rev t-since-cxx14">(since C++14)</span> </td>
</tr>
<tr class="t-dcl">
<td> <div><span class="mw-geshi cpp source-cpp">ClosureType<span class="br0">(</span>ClosureType<span class="sy3">&&</span> <span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad"> </td>
<td> <span class="t-mark-rev t-since-cxx14">(since C++14)</span> </td>
</tr>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>Closure types are not <a href="../concept/DefaultConstructible.html" title="cpp/concept/DefaultConstructible"><code>DefaultConstructible</code></a>. Closure types have <span class="t-rev-inl t-until-cxx14"><span>a deleted</span> <span><span class="t-mark-rev t-until-cxx14">(until C++14)</span></span></span><span class="t-rev-inl t-since-cxx14"><span>no</span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span> default constructor. The copy constructor and the move constructor are <span class="t-rev-inl t-until-cxx14"><span>implicitly-declared</span> <span><span class="t-mark-rev t-until-cxx14">(until C++14)</span></span></span><span class="t-rev-inl t-since-cxx14"><span>declared as defaulted</span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span> and may be implicitly-defined according to the usual rules for <a href="copy_constructor.html" title="cpp/language/copy constructor">copy constructors</a> and <a href="move_constructor.html" title="cpp/language/move constructor">move constructors</a>.
</p>
</div>
<div class="t-member">
<h2> <span class="mw-headline" id="ClosureType::operator.3D.28const_ClosureType.26.29"> <span style="font-size:0.7em; line-height:130%">ClosureType::</span>operator=(const ClosureType&) </span></h2>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">ClosureType<span class="sy3">&</span> operator<span class="sy1">=</span><span class="br0">(</span><span class="kw4">const</span> ClosureType<span class="sy3">&</span><span class="br0">)</span> <span class="sy1">=</span> delete<span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad"> </td>
<td class="t-dcl-nopad"> </td>
</tr>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>Closure types are not <a href="../concept/CopyAssignable.html" title="cpp/concept/CopyAssignable"><code>CopyAssignable</code></a>.
</p>
</div>
<div class="t-member">
<h2> <span class="mw-headline" id="ClosureType::.7EClosureType.28.29"> <span style="font-size:0.7em; line-height:130%">ClosureType::</span>~ClosureType() </span></h2>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">~ClosureType<span class="br0">(</span><span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad"> </td>
<td class="t-dcl-nopad"> </td>
</tr>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>The destructor is implicitly-declared.
</p>
</div>
<div class="t-member">
<h2> <span class="mw-headline" id="ClosureType::Captures"> <span style="font-size:0.7em; line-height:130%">ClosureType::</span><span class="t-spar">Captures</span> </span></h2>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">T1 a<span class="sy4">;</span><br />
<p>T2 b<span class="sy4">;</span><br />
</p>
...</span></div></td>
<td class="t-dcl-nopad"> </td>
<td class="t-dcl-nopad"> </td>
</tr>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>If the lambda-expression captures anything by copy (either implicitly with capture clause <code><b>[=]</b></code> or explicitly with a capture that does not include the character &, e.g. <code><b>[a, b, c]</b></code>), the closure type includes unnamed non-static data members, declared in unspecified order, that hold copies of all entities that were so captured.
</p><p>Those data members that correspond to captures without initializers are <a href="direct_initialization.html" title="cpp/language/direct initialization">direct-initialized</a> when the lambda-expression is evaluated. Those that correspond to captures with initializers are initialized as the initializer requires (could be copy- or direct-initialization). If an array is captured, array elements are direct-initialized in increasing index order. The order in which the data members are initialized is the order in which they are declared (which is unspecified).
</p><p>The type of each data member is the type of the corresponding captured entity, except if the entity has reference type (in that case, references to functions are captured as-is, and references to objects are captured as copies of the referenced objects).
</p><p>For the entities that are captured by reference (with the default capture <code><b>[&]</b></code> or when using the character &, e.g. <code><b>[&a, &b, &c]</b></code>), it is unspecified if additional data members are declared in the closure type.
</p>
</div>
<p>Lambda-expressions are not allowed in <a href="expressions.html#Unevaluated_expressions" title="cpp/language/expressions">unevaluated expressions</a>, <a href="template_parameters.html" title="cpp/language/template parameters">template arguments</a>, <a href="type_alias.html" title="cpp/language/type alias">alias declarations</a>, <a href="typedef.html" title="cpp/language/typedef">typedef declarations</a>, and anywhere in a function (or function template) declaration except the function body and the function's <a href="default_arguments.html" title="cpp/language/default arguments">default arguments</a>.
</p>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=edit&section=4" title="Edit section: Lambda capture">edit</a>]</span> <span class="mw-headline" id="Lambda_capture">Lambda capture</span></h3>
<p>The <span class="t-spar">capture-list</span> is a comma-separated list of zero or more <i>captures</i>, optionally beginning with the <span class="t-spar">capture-default</span>. The only capture defaults are <code><b>&</b></code> (implicitly catch the <a href="definition.html#ODR-use" title="cpp/language/definition">odr-used</a> automatic variables and <code>this</code> by reference) and <code><b> = </b></code> (implicitly catch the odr-used automatic variables and <code>this</code> by value).
</p><p>The syntax of an individual capture in <span class="t-spar">capture-list</span> is
</p>
<table class="t-sdsc-begin">
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">identifier</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">identifier</span> <code><b>...</b></code>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">identifier</span> <span class="t-spar">initializer</span>
</td>
<td> (3)
</td>
<td> <span class="t-mark-rev t-since-cxx14">(C++14)</span>
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>&</b></code> <span class="t-spar">identifier</span>
</td>
<td> (4)
</td>
<td class="t-sdsc-nopad">
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>&</b></code> <span class="t-spar">identifier</span> <code><b>...</b></code>
</td>
<td> (5)
</td>
<td class="t-sdsc-nopad">
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>&</b></code> <span class="t-spar">identifier</span> <span class="t-spar">initializer</span>
</td>
<td> (6)
</td>
<td> <span class="t-mark-rev t-since-cxx14">(C++14)</span>
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>this</b></code>
</td>
<td> (7)
</td>
<td class="t-sdsc-nopad">
</td></tr>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> simple by-copy capture</div>
<div class="t-li1"><span class="t-li">2)</span> by-copy capture that is a <a href="parameter_pack.html" title="cpp/language/parameter pack">pack expansion</a></div>
<div class="t-li1"><span class="t-li">3)</span> by-copy capture with an <a href="initialization.html" title="cpp/language/initialization">initializer</a></div>
<div class="t-li1"><span class="t-li">4)</span> simple by-reference capture</div>
<div class="t-li1"><span class="t-li">5)</span> by-reference capture that is a <a href="parameter_pack.html" title="cpp/language/parameter pack">pack expansion</a></div>
<div class="t-li1"><span class="t-li">6)</span> by-reference capture with an initializer</div>
<div class="t-li1"><span class="t-li">7)</span> capture for the <a href="this.html" title="cpp/language/this">this pointer</a></div>
<p>If a capture-default is used, no other captures may use the same capture type. Any capture may appear only once.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> S2 <span class="br0">{</span> <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">void</span> S2<span class="sy4">::</span><span class="me2">f</span><span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span>
<span class="br0">{</span>
<span class="br0">[</span><span class="sy3">&</span>, i<span class="br0">]</span> <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// ok</span>
<span class="br0">[</span><span class="sy3">&</span>, <span class="sy3">&</span>i<span class="br0">]</span> <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// error: i preceded by & when & is the default</span>
<span class="br0">[</span><span class="sy1">=</span>, this<span class="br0">]</span> <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// error: this when = is the default</span>
<span class="br0">[</span>i, i<span class="br0">]</span> <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// error: i repeated</span>
<span class="br0">}</span></pre></div></div>
<p>Only lambda-expressions defined at block scope may have a capture-default or captures without initializers. For such lambda-expression, the <i>reaching scope</i> is defined as the set of enclosing scopes up to and including the innermost enclosing function (and its parameters). This includes nested block scopes and the scopes of enclosing lambdas if this lambda is nested.
</p><p>The <span class="t-spar">identifier</span> in any capture without an initializer (other than the <code>this</code>-capture) is looked up using usual <a href="lookup.html" title="cpp/language/lookup">unqualified name lookup</a> in the <i>reaching scope</i> of the lambda. Such name is <i>explicitly captured</i>.
</p>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<p>A capture with an initializer acts as if it declares and explicitly captures a variable declared with type <a href="auto.html" title="cpp/language/auto">auto</a>, whose declarative region is the body of the lambda expression (that is, it is not in scope within its initializer), except that:
</p>
<ul><li> if the capture is by-copy, the non-static data member of the closure object is another way to refer to that auto variable.
</li><li> if the capture is by-reference, the reference variable's lifetime ends when the lifetime of the closure object ends
</li></ul>
<p>This is used to capture move-only types with a capture such as <span class="t-c"><span class="mw-geshi cpp source-cpp">x <span class="sy1">=</span> std<span class="sy4">::</span><span class="me2">move</span><span class="br0">(</span>x<span class="br0">)</span></span></span>
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> x <span class="sy1">=</span> <span class="nu0">4</span><span class="sy4">;</span>
<span class="kw4">auto</span> y <span class="sy1">=</span> <span class="br0">[</span><span class="sy3">&</span>r <span class="sy1">=</span> x, x <span class="sy1">=</span> x <span class="sy2">+</span> <span class="nu0">1</span><span class="br0">]</span><span class="br0">(</span><span class="br0">)</span><span class="sy2">-</span><span class="sy1">></span><span class="kw4">int</span>
<span class="br0">{</span>
r <span class="sy2">+</span><span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
<span class="kw1">return</span> x <span class="sy2">+</span> <span class="nu0">2</span><span class="sy4">;</span>
<span class="br0">}</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// updates ::x to 6 and initializes y to 7.</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<p>If a capture list has a capture-default and does not explicitly capture <code>this</code> or an automatic variable, captures it <i>implicitly</i> if
</p>
<ul><li> the body of the lambda <a href="definition.html#ODR-use" title="cpp/language/definition">odr-uses</a> the variable or <code>this</code>
</li></ul>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<ul><li> or the variable or <code>this</code> is named in a potentially-evaluated expression within an expression that depends on a generic lambda parameter
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">const</span> <span class="kw4">int</span> <span class="br0">(</span><span class="sy3">&</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="br0">}</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span> <span class="co1">// #1</span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">int</span><span class="sy3">&</span>, <span class="kw4">const</span> <span class="kw4">int</span> <span class="br0">(</span><span class="sy3">&</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">1</span><span class="br0">]</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span> <span class="co1">// #2</span>
<span class="kw4">void</span> test<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="kw4">const</span> <span class="kw4">int</span> x <span class="sy1">=</span> <span class="nu0">17</span><span class="sy4">;</span>
<span class="kw4">auto</span> g1 <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> a<span class="br0">)</span> <span class="br0">{</span> f<span class="br0">(</span>x<span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// ok: calls #1, does not capture x</span>
<span class="kw4">auto</span> g2 <span class="sy1">=</span> <span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> a<span class="br0">)</span>
<span class="br0">{</span>
<span class="kw4">int</span> selector<span class="br0">[</span>sizeof<span class="br0">(</span>a<span class="br0">)</span> <span class="sy1">==</span> <span class="nu0">1</span> <span class="sy4">?</span> <span class="nu0">1</span> <span class="sy4">:</span> <span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
f<span class="br0">(</span>x, selector<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// ok: is a dependent expression, so captures x</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<p>If the body of a lambda <a href="definition.html#ODR-use" title="cpp/language/definition">odr-uses</a> an entity captured by copy, the member of the closure type is accessed. If it is not odr-using the entity, the access is to the original object:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">int</span><span class="sy2">*</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="kw4">const</span> <span class="kw4">int</span> N <span class="sy1">=</span> <span class="nu0">10</span><span class="sy4">;</span>
<span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span>
<span class="br0">{</span>
<span class="kw4">int</span> arr<span class="br0">[</span>N<span class="br0">]</span><span class="sy4">;</span> <span class="co1">// not an odr-use: refers to g's const int N</span>
f<span class="br0">(</span><span class="sy3">&</span>N<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// odr-use: causes N to be captured (by copy)</span>
<span class="co1">// &N is the address of the closure object's member N, not g's N</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>Within the body of a lambda, any use of <a href="decltype.html" title="cpp/language/decltype">decltype</a> on any variable with automatic storage duration is as if it were captured and odr-used, even though decltype itself isn't an odr-use and no actual capture takes place:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f3<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
<span class="kw4">float</span> x, <span class="sy3">&</span>r <span class="sy1">=</span> x<span class="sy4">;</span>
<span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span>
<span class="br0">{</span> <span class="co1">// x and r are not captured (appearance in a decltype operand is not an odr-use)</span>
decltype<span class="br0">(</span>x<span class="br0">)</span> y1<span class="sy4">;</span> <span class="co1">// y1 has type float</span>
decltype<span class="br0">(</span><span class="br0">(</span>x<span class="br0">)</span><span class="br0">)</span> y2 <span class="sy1">=</span> y1<span class="sy4">;</span> <span class="co1">// y2 has type float const& because this lambda</span>
<span class="co1">// is not mutable and x is an lvalue</span>
decltype<span class="br0">(</span>r<span class="br0">)</span> r1 <span class="sy1">=</span> y1<span class="sy4">;</span> <span class="co1">// r1 has type float& (transformation not considered)</span>
decltype<span class="br0">(</span><span class="br0">(</span>r<span class="br0">)</span><span class="br0">)</span> r2 <span class="sy1">=</span> y2<span class="sy4">;</span> <span class="co1">// r2 has type float const&</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>Any entity captured by a lambda (implicitly or explicitly) is odr-used by the lambda-expression (therefore, Implicit capture by a nested lambda triggers implicit capture in the enclosing lambda).
</p><p>All implicitly-captured variables must be declared within the <i>reaching scope</i> of the lambda.
</p><p>If a lambda captures <code>this</code>, the nearest enclosing function must be a non-static member function.
</p><p>If a generic lambda (or an instantiation of a generic capture-less lambda's conversion operator) ODR-uses <code>this</code> or any variable, it is captured by the lambda expression.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f1<span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span>
<span class="br0">{</span>
<span class="kw4">int</span> <span class="kw4">const</span> N <span class="sy1">=</span> <span class="nu0">20</span><span class="sy4">;</span>
<span class="kw4">auto</span> m1 <span class="sy1">=</span> <span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span>
<span class="br0">{</span>
<span class="kw4">int</span> <span class="kw4">const</span> M <span class="sy1">=</span> <span class="nu0">30</span><span class="sy4">;</span>
<span class="kw4">auto</span> m2 <span class="sy1">=</span> <span class="br0">[</span>i<span class="br0">]</span>
<span class="br0">{</span>
<span class="kw4">int</span> x<span class="br0">[</span>N<span class="br0">]</span><span class="br0">[</span>M<span class="br0">]</span><span class="sy4">;</span> <span class="co1">// N and M are not odr-used </span>
<span class="co1">// (ok that they are not captured)</span>
x<span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span> <span class="sy1">=</span> i<span class="sy4">;</span> <span class="co1">// i is explicitly captured by m2</span>
<span class="co1">// and implicitly captured by m1</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw1">struct</span> s1 <span class="co1">// local class within f1()</span>
<span class="br0">{</span>
<span class="kw4">int</span> f<span class="sy4">;</span>
<span class="kw4">void</span> work<span class="br0">(</span><span class="kw4">int</span> n<span class="br0">)</span> <span class="co1">// non-static member function</span>
<span class="br0">{</span>
<span class="kw4">int</span> m <span class="sy1">=</span> n <span class="sy2">*</span> n<span class="sy4">;</span>
<span class="kw4">int</span> j <span class="sy1">=</span> <span class="nu0">40</span><span class="sy4">;</span>
<span class="kw4">auto</span> m3 <span class="sy1">=</span> <span class="br0">[</span>this, m<span class="br0">]</span>
<span class="br0">{</span>
<span class="kw4">auto</span> m4 <span class="sy1">=</span> <span class="br0">[</span><span class="sy3">&</span>, j<span class="br0">]</span> <span class="co1">// error: j is not captured by m3</span>
<span class="br0">{</span>
<span class="kw4">int</span> x <span class="sy1">=</span> n<span class="sy4">;</span> <span class="co1">// error: n is implicitly captured by m4</span>
<span class="co1">// but not captured by m3</span>
x <span class="sy2">+</span><span class="sy1">=</span> m<span class="sy4">;</span> <span class="co1">// ok: m is implicitly captured by m4</span>
<span class="co1">// and explicitly captured by m3</span>
x <span class="sy2">+</span><span class="sy1">=</span> i<span class="sy4">;</span> <span class="co1">// error: i is outside of the reaching scope</span>
<span class="co1">// (which ends at work())</span>
x <span class="sy2">+</span><span class="sy1">=</span> f<span class="sy4">;</span> <span class="co1">// ok: this is captured implicitly by m4</span>
<span class="co1">// and explicitly captured by m3</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>If a lambda-expression appears in a <a href="default_arguments.html" title="cpp/language/default arguments">default argument</a>, it cannot explicitly or implicitly capture anything.
</p><p>Members of <a href="union.html" title="cpp/language/union">anonymous unions</a> cannot be captured.
</p><p>If a nested lambda <code>m2</code> captures something that is also captured by the immediately enclosing lambda <code>m1</code>, then <code>m2</code>'s capture is transformed as follows:
</p>
<ul><li> if the enclosing lambda <code>m1</code> captures by copy, <code>m2</code> is capturing the non-static member of <code>m1</code>'s closure type, not the original variable or <code>this</code>.
</li><li> if the enclosing lambda <code>m1</code> by reference, <code>m2</code> is capturing the original variable or <code>this</code>.
</li></ul>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">Run this code</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include <iostream></span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="kw4">int</span> a <span class="sy1">=</span> <span class="nu0">1</span>, b <span class="sy1">=</span> <span class="nu0">1</span>, c <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
 
<span class="kw4">auto</span> m1 <span class="sy1">=</span> <span class="br0">[</span>a, <span class="sy3">&</span>b, <span class="sy3">&</span>c<span class="br0">]</span><span class="br0">(</span><span class="br0">)</span> mutable
<span class="br0">{</span>
<span class="kw4">auto</span> m2 <span class="sy1">=</span> <span class="br0">[</span>a, b, <span class="sy3">&</span>c<span class="br0">]</span><span class="br0">(</span><span class="br0">)</span> mutable
<span class="br0">{</span>
<a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> a <span class="sy1"><<</span> b <span class="sy1"><<</span> c <span class="sy1"><<</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
a <span class="sy1">=</span> <span class="nu0">4</span><span class="sy4">;</span> b <span class="sy1">=</span> <span class="nu0">4</span><span class="sy4">;</span> c <span class="sy1">=</span> <span class="nu0">4</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
 
a <span class="sy1">=</span> <span class="nu0">3</span><span class="sy4">;</span> b <span class="sy1">=</span> <span class="nu0">3</span><span class="sy4">;</span> c <span class="sy1">=</span> <span class="nu0">3</span><span class="sy4">;</span>
m2<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
 
a <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span> b <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span> c <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
 
m1<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// calls m2() and prints 123</span>
<a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> a <span class="sy1"><<</span> b <span class="sy1"><<</span> c <span class="sy1"><<</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span> <span class="co1">// prints 234</span>
<span class="br0">}</span></pre></div></div>
<p><br />
</p>
</div>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=edit&section=5" title="Edit section: Example">edit</a>]</span> <span class="mw-headline" id="Example">Example</span></h3>
<div class="t-example"><p> This example shows (a) how to pass a lambda to a generic algorithm; (b) how objects resulting from a lambda declaration can be stored in <span class="t-lc"><a href="../utility/functional/function.html" title="cpp/utility/functional/function">std::function</a></span> objects.
</p><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">Run this code</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include <vector></span>
<span class="co2">#include <iostream></span>
<span class="co2">#include <algorithm></span>
<span class="co2">#include <functional></span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<a href="../container/vector.html"><span class="kw1094">std::<span class="me2">vector</span></span></a><span class="sy1"><</span><span class="kw4">int</span><span class="sy1">></span> c <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">4</span>, <span class="nu0">5</span>, <span class="nu0">6</span>, <span class="nu0">7</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">int</span> x <span class="sy1">=</span> <span class="nu0">5</span><span class="sy4">;</span>
c.<span class="me1">erase</span><span class="br0">(</span><a href="../algorithm/remove.html"><span class="kw1326">std::<span class="me2">remove_if</span></span></a><span class="br0">(</span>c.<span class="me1">begin</span><span class="br0">(</span><span class="br0">)</span>, c.<span class="me1">end</span><span class="br0">(</span><span class="br0">)</span>, <span class="br0">[</span>x<span class="br0">]</span><span class="br0">(</span><span class="kw4">int</span> n<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> n <span class="sy1"><</span> x<span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span>, c.<span class="me1">end</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
 
<a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> <span class="st0">"c: "</span><span class="sy4">;</span>
<a href="../algorithm/for_each.html"><span class="kw1302">std::<span class="me2">for_each</span></span></a><span class="br0">(</span>c.<span class="me1">begin</span><span class="br0">(</span><span class="br0">)</span>, c.<span class="me1">end</span><span class="br0">(</span><span class="br0">)</span>, <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span><span class="br0">{</span> <a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> i <span class="sy1"><<</span> <span class="st0">' '</span><span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span>
<a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
 
<span class="co1">// the type of a closure cannot be named, but can be inferred with auto</span>
<span class="kw4">auto</span> func1 <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> i <span class="sy2">+</span> <span class="nu0">4</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> <span class="st0">"func1: "</span> <span class="sy1"><<</span> func1<span class="br0">(</span><span class="nu0">6</span><span class="br0">)</span> <span class="sy1"><<</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
 
<span class="co1">// like all callable objects, closures can be captured in std::function</span>
<span class="co1">// (this may incur unnecessary overhead)</span>
<a href="../utility/functional/function.html"><span class="kw930">std::<span class="me2">function</span></span></a><span class="sy1"><</span><span class="kw4">int</span><span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy1">></span> func2 <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> i <span class="sy2">+</span> <span class="nu0">4</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> <span class="st0">"func2: "</span> <span class="sy1"><<</span> func2<span class="br0">(</span><span class="nu0">6</span><span class="br0">)</span> <span class="sy1"><<</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>Output:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">c: 5 6 7
func1: 10
func2: 10</pre></div></div>
</div>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=edit&section=6" title="Edit section: Defect reports">edit</a>]</span> <span class="mw-headline" id="Defect_reports">Defect reports</span></h3>
<p>The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> Applied to
</th>
<th> Behavior as published
</th>
<th> Correct behavior
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1891">CWG 1891</a>
</td>
<td> C++14
</td>
<td> closure had no default ctor and implicit copy/move ctors
</td>
<td> deleted default and defaulted copy/move
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1722">CWG 1722</a>
</td>
<td> C++14
</td>
<td> the conversion function for captureless lambdas had unspecified exception specification
</td>
<td> conversion function is noexcept
</td></tr></table>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=edit&section=7" title="Edit section: See also">edit</a>]</span> <span class="mw-headline" id="See_also">See also</span></h3>
<table class="t-dsc-begin">
<tr class="t-dsc">
<td> <a href="auto.html" title="cpp/language/auto"> auto specifier </a>
</td>
<td> specifies a type defined by an expression <span class="t-mark-rev t-since-cxx11">(C++11)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/language/dsc_auto&action=edit">[edit]</a></span>
</td></tr>
<tr class="t-dsc">
<td> <div class="t-dsc-member-div"><div><a href="../utility/functional/function.html" title="cpp/utility/functional/function"> <span class="t-lines"><span>function</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div>
</td>
<td> wraps callable object of any type with specified function call signature <br /> <span class="t-mark">(class template)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/utility/functional/dsc_function&action=edit">[edit]</a></span>
</td></tr>
</table>
<!--
NewPP limit report
Preprocessor visited node count: 4670/1000000
Preprocessor generated node count: 10424/1000000
Post‐expand include size: 77952/2097152 bytes
Template argument size: 38284/2097152 bytes
Highest expansion depth: 20/40
Expensive parser function count: 0/100
-->
<!-- Saved in parser cache with key mwiki1-mwiki_en_:pcache:idhash:5059-0!*!0!!en!*!* and timestamp 20151103153428 -->
</div> <!-- /bodycontent -->
<!-- printfooter -->
<div class="printfooter">
Retrieved from "<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&oldid=81678">http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&oldid=81678</a>" </div>
<!-- /printfooter -->
<!-- catlinks -->
<div id='catlinks' class='catlinks catlinks-allhidden'></div> <!-- /catlinks -->
<div class="visualClear"></div>
<!-- debughtml -->
<!-- /debughtml -->
</div>
<!-- /bodyContent -->
</div>
</div>
<!-- /content -->
<!-- footer -->
<div id="cpp-footer-base" class="noprint">
<div id="footer">
<div id="cpp-navigation">
<h5>Navigation</h5>
<ul>
<li id="n-Support-us"><a href="http://www.cppreference.com/support" rel="nofollow">Support us</a></li><li id="n-recentchanges"><a href="http://en.cppreference.com/w/Special:RecentChanges" title="A list of recent changes in the wiki [r]" accesskey="r">Recent changes</a></li><li id="n-FAQ"><a href="http://en.cppreference.com/w/Cppreference:FAQ">FAQ</a></li><li id="n-Offline-version"><a href="http://en.cppreference.com/w/Cppreference:Archives">Offline version</a></li> </ul>
</div>
<div id="cpp-toolbox">
<h5><span>Toolbox</span><a href="lambda.html#"></a></h5>
<ul>
<li id="t-whatlinkshere"><a href="http://en.cppreference.com/w/Special:WhatLinksHere/cpp/language/lambda" title="A list of all wiki pages that link here [j]" accesskey="j">What links here</a></li><li id="t-recentchangeslinked"><a href="http://en.cppreference.com/w/Special:RecentChangesLinked/cpp/language/lambda" title="Recent changes in pages linked from this page [k]" accesskey="k">Related changes</a></li><li id="t-upload"><a href="http://upload.cppreference.com/w/Special:Upload" title="Upload files [u]" accesskey="u">Upload file</a></li><li id="t-specialpages"><a href="http://en.cppreference.com/w/Special:SpecialPages" title="A list of all special pages [q]" accesskey="q">Special pages</a></li><li id="t-print"><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&printable=yes" rel="alternate" title="Printable version of this page [p]" accesskey="p">Printable version</a></li><li id="t-permalink"><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&oldid=81678" title="Permanent link to this revision of the page">Permanent link</a></li><li id="t-info"><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&action=info">Page information</a></li> </ul>
</div>
<div id="cpp-languages">
<div><ul><li>In other languages</li></ul></div>
<div><ul>
<li class="interwiki-de"><a href="http://de.cppreference.com/w/cpp/language/lambda" title="cpp/language/lambda" lang="de" hreflang="de">Deutsch</a></li><li class="interwiki-es"><a href="http://es.cppreference.com/w/cpp/language/lambda" title="cpp/language/lambda" lang="es" hreflang="es">Español</a></li><li class="interwiki-fr"><a href="http://fr.cppreference.com/w/cpp/language/lambda" title="cpp/language/lambda" lang="fr" hreflang="fr">Français</a></li><li class="interwiki-it"><a href="http://it.cppreference.com/w/cpp/language/lambda" title="cpp/language/lambda" lang="it" hreflang="it">Italiano</a></li><li class="interwiki-ja"><a href="http://ja.cppreference.com/w/cpp/language/lambda" title="cpp/language/lambda" lang="ja" hreflang="ja">日本語</a></li><li class="interwiki-pt"><a href="http://pt.cppreference.com/w/cpp/language/lambda" title="cpp/language/lambda" lang="pt" hreflang="pt">Português</a></li><li class="interwiki-ru"><a href="http://ru.cppreference.com/w/cpp/language/lambda" title="cpp/language/lambda" lang="ru" hreflang="ru">Русский</a></li><li class="interwiki-zh"><a href="http://zh.cppreference.com/w/cpp/language/lambda" title="cpp/language/lambda" lang="zh" hreflang="zh">中文</a></li> </ul></div>
</div>
<ul id="footer-info">
<li id="footer-info-lastmod"> This page was last modified on 3 November 2015, at 07:34.</li>
<li id="footer-info-viewcount">This page has been accessed 374,089 times.</li>
</ul>
<ul id="footer-places">
<li id="footer-places-privacy"><a href="http://en.cppreference.com/w/Cppreference:Privacy_policy" title="Cppreference:Privacy policy">Privacy policy</a></li>
<li id="footer-places-about"><a href="http://en.cppreference.com/w/Cppreference:About" title="Cppreference:About">About cppreference.com</a></li>
<li id="footer-places-disclaimer"><a href="http://en.cppreference.com/w/Cppreference:General_disclaimer" title="Cppreference:General disclaimer">Disclaimers</a></li>
</ul>
<ul id="footer-icons" class="noprint">
<li id="footer-poweredbyico">
<a href="http://www.mediawiki.org/"><img src="../../../mwiki/skins/common/images/poweredby_mediawiki_88x31.png" alt="Powered by MediaWiki" width="88" height="31" /></a> <a href="http://qbnz.com/highlighter/"><img src="../../../../upload.cppreference.com/mwiki/images/2/2b/powered_by_geshi_88x31.png" alt="Powered by GeSHi" height="31" width="88" /></a> <a href="http://www.tigertech.net/referral/cppreference.com"><img src="../../../../upload.cppreference.com/mwiki/images/9/94/powered_by_tigertech_88x31.png" alt="Hosted by Tiger Technologies" height="31" width="88" /></a> </li>
</ul>
<div style="clear:both">
</div>
</div>
</div>
<!-- /footer -->
<script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../mwiki/load.php%3Fdebug=false&lang=en&modules=skins.cppreference2&only=scripts&skin=cppreference2&*"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../mwiki/load.php%3Fdebug=false&lang=en&modules=site&only=scripts&skin=cppreference2&*"></script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.082 secs. -->
</body>
<!-- Cached 20151103153453 -->
</html>
|