반응형
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
■ 컴파일 및 실행해보기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// | |
// 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(); | |
} |
반응형
'C++ > 0x09-lambda express' 카테고리의 다른 글
lambda express 사용하기 ][ the call operator (0) | 2019.02.27 |
---|