본문 바로가기
C++/0x09-lambda express

lambda express 사용하기 ][ captures

by SpeeDr00t 2019. 2. 27.
반응형


lambda express 사용하기 ][ captures


test해보기 : https://wandbox.org/permlink/fWfHGJXzDpMEBzjX


■  the basic syntax

[&] : capture by reference , all automatic storage duration variable declared in th reaching scope

[=] : capture by value, a value is copied

[x,&y] : capture x by value and y by a reference explicitly



■ 컴파일 및 실행해보기

//
//
// g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.69.0/gcc-8.3.0/include -std=c++17
//
//
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int x = 1, y = 1;
std::cout << x << " " << y << std::endl;
auto func1 = [&x, &y]() { cout << ++x << " " << ++y << endl; };
func1();
auto func2 = [=]() mutable{ cout << ++x << " " << endl; };
func2();
auto func3 = [&]() mutable{ cout << ++x << " " << endl; };
func3();
auto func4 = [x, &y]()mutable { cout << ++x << " " << ++y << endl; };
func4();
}
view raw Captures.cpp hosted with ❤ by GitHub

 


반응형

'C++ > 0x09-lambda express' 카테고리의 다른 글

lambda express 사용하기 ][ the call operator  (0) 2019.02.27