cpp 0x kimryungee

31
C++ 0x 달려 BOA~ ..사 비밀 모임 발표 : 김연기

Upload: scor7910

Post on 15-Jan-2015

1.012 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Cpp 0x kimRyungee

C++ 0x 달려 BOA요~

아.꿈.사 비밀 모임

발표 : 김연기

Page 2: Cpp 0x kimRyungee

발표자 는 뉴규?

김연기아.꿈.사 오후반 스터디 그룹 장소 예약 담당(Pattern Oriented Software Architecture 2)

2008. 10 ~ Microsoft Visual C++ MVP

유콘시스템 Sw개발팀 지상관제 장비 SW 개발잉카 인터넷 보안개발팀 업데이트 모듈 개발.

http://twitter.com/scor7910http://scor7910.tistory.com

Page 3: Cpp 0x kimRyungee

차례

• 새롭게 추가 된 것들.

– Lambda

– R-Value Reference

– auto, decltype, constexpr

– Smart Pointer

• 참고자료.

• Q&A

Page 4: Cpp 0x kimRyungee

Lambda

• 명명되지 않은(Unnamed) 함수 객체.

Page 5: Cpp 0x kimRyungee

Lambda

• 명명되지 않은(Unnamed) 함수 객체.

Lambda Introducer

Page 6: Cpp 0x kimRyungee

Lambda

• 명명되지 않은(Unnamed) 함수 객체.

Lambda Parameter declaration

Page 7: Cpp 0x kimRyungee

Lambda

• 명명되지 않은(Unnamed) 함수 객체.

Lambda Compound Statement

Page 8: Cpp 0x kimRyungee

Lambda

• 명명되지 않은(Unnamed) 함수 객체.

Lambda Return Type

Page 9: Cpp 0x kimRyungee

Lambda –사용-int main() {vector<int> v;

for (int i = 0; i < 10; ++i){

v.push_back(i);}

for_each(v.begin(), v.end(), [](int n) { cout << n << " "; });

cout << endl;}

Page 10: Cpp 0x kimRyungee

Lambda –함수객체-

struct LambdaFunctor {void operator()(int n) const {

cout << n << " ";}

};

…….

for_each(v.begin(), v.end(), LambdaFunctor() );

Page 11: Cpp 0x kimRyungee

Lambda –리턴-

• []()->리턴 타입{…}

transform(v.begin(), v.end(), front_inserter(d), [](int n) -> double {

if (n % 2 == 0) {

return n * n * n;} else{

return n / 2.0;}

});

Page 12: Cpp 0x kimRyungee

Lambda –캡쳐-

• 상위 스코프({…})의 변수를 람다 구현내부에서 사용할 수 있다.

• [변수1, 변수2] : 변수1, 변수2 캡쳐

• [&변수1, &변수2] : 변수1, 변수2 참조캡쳐

• [&] : 상위 스코프의 변수를 참조 캡쳐.

• [=] : 상위 스코프의 변수를 값 캡쳐.

Page 13: Cpp 0x kimRyungee

Lambda –캡쳐-int x = 4;int y = 5;cout << "Input: ";cin >> x >> y;

v.erase(remove_if(v.begin(), v.end(), [x, y](int n) { return x < n && n < y; }), v.end());

int x = 4;int y = 5;for_each(v.begin(), v.end(), [=](int& r) mutable {//값을 캡쳐하면 x,y는 const로 들어오지만 mutable 키워드로//캡쳐된 변수를 변경가능한 변수로 만들어줌.

const int old = r;r *= x * y;x = y;y = old;

});

Page 14: Cpp 0x kimRyungee

Lambda –캡쳐-int x = 4;int y = 5;cout << "Input: ";cin >> x >> y;

v.erase(remove_if(v.begin(), v.end(), [&x, &y](int n) { return x < n && n < y; }), v.end());

int x = 4;int y = 5;for_each(v.begin(), v.end(), [&](int& r) {

const int old = r;r *= x * y;x = y;y = old;

});

Page 15: Cpp 0x kimRyungee

R-Value Reference

• L-Value & R-Value

res = (++a

+ b++);

a=10;

b =13;

Page 16: Cpp 0x kimRyungee

R-Value Reference

• L-Value & R-Value

res = (++a

+ b++);

a=10;

b =13;

ab

res++a

1013

b++(++a + b++)

Page 17: Cpp 0x kimRyungee

R-Value Reference

• L-Value & R-Value

res = (++a

+ b++);

a=10;

b =13;

ab

res++a

1013

b++(++a + b++)

Page 18: Cpp 0x kimRyungee

R-Value Reference

double& rd1 = 2.0; const double& rd2 = 2.0;double&& rd3 = 2.0;

rd2 += 1;rd3 +=3;

Page 19: Cpp 0x kimRyungee

R-Value Reference

double& rd1 = 2.0; ERROR!! const double& rd2 = 2.0;double&& rd3 = 2.0;

rd2 += 1; ERROR!!rd3 +=3;

Page 20: Cpp 0x kimRyungee

R-Value Reference –Move-class SomeThing{

public:SomeThing() {}SomeThing(SomeThing& s){

memcpy(&data_, &s.data_, sizeof(A));

}SomeThing(SomeThing&& s){

data_ = move(s.data_);}~SomeThing(){}A data_;

};

int main(){

SomeThing S1;S1.data_.a = 0;S1.data_.b = 10;strcpy(S1.data_.c, "KimRyungee");

SomeThing S2(S1);SomeThing&&

S3(forward<SomeThing&&>(S1));SomeThing& S4 = S2;return 0;

}

Page 21: Cpp 0x kimRyungee

auto

for (vector<int>::const_iterator itr = myvec.begin(); itr != myvec.end(); ++itr)

for (auto itr = myvec.begin(); itr != myvec.end(); ++itr)

Page 22: Cpp 0x kimRyungee

decltype

const int&& foo();int i;struct A { double x; };const A* a = new A();

decltype(foo()) x1 = i; // x1 타입은 const int&&decltype(i) x2; // x2 타입은 intdecltype(a->x) x3; // x3 타입은 doubledecltype((a->x)) x4 = x3; // x4 타입은 double&

Page 23: Cpp 0x kimRyungee

constexpr

constexptr GetBufferSize();

TCHAR buffer[GetBufferSize ()+ 12];

Page 24: Cpp 0x kimRyungee

Smart Pointer –auto_ptr-class LessPtr{public:

bool operator () ( auto_ptr<int> ptr1,auto_ptr<int> ptr2) const

{return *(ptr1.get()) >

*(ptr2.get()) ? true : false;

}};

int main(){

vector< auto_ptr<int> > arrInt;vector< auto_ptr<int> >::iterator

pos = arrInt.begin();

/*생략*/sort(arrInt.begin(), arrInt.end(),

LessPtr());pos = arrInt.begin();for(; pos != arrInt.end(); ++pos){

cout<<" "<<(*pos).get()<<" ";}

return 0;}

Page 25: Cpp 0x kimRyungee

Smart Pointer –auto_ptr-class LessPtr{public:

bool operator () ( auto_ptr<int> ptr1,auto_ptr<int> ptr2) const

{return *(ptr1.get()) >

*(ptr2.get()) ? true : false;

}};

int main(){

vector< auto_ptr<int> > arrInt;vector< auto_ptr<int> >::iterator

pos = arrInt.begin();

/*생략*/sort(arrInt.begin(), arrInt.end(),

LessPtr());pos = arrInt.begin();for(; pos != arrInt.end(); ++pos){

cout<<" "<<(*pos).get()<<" ";}

return 0;}

Page 26: Cpp 0x kimRyungee

Smart Pointer –auto_ptr-

template<class _Other>_Myt& operator=(auto_ptr<_Other>& _Right) _THROW0(){

// assign compatible _Right (assume pointer)reset(_Right.release());return (*this);

}

Page 27: Cpp 0x kimRyungee

Smart Pointer –shared_ptr-class LessSharedPtr{public:

bool operator () ( shared_ptr<int> ptr1,shared_ptr<int> ptr2)

const{

return *(ptr1.get()) >*(ptr2.get()) ? true : false;

}};

int main(){

vector< shared_ptr<int> > arrInt;vector< shared_ptr<int> >::iterator

pos = arrInt.begin();

/*생략*/sort(arrInt.begin(), arrInt.end(),

LessSharedPtr());pos = arrInt.begin();for(; pos != arrInt.end(); ++pos){

cout<<" "<<*((*pos).get())<<" ";

}return 0;

}

Page 28: Cpp 0x kimRyungee

Smart Pointer -weak_ptr-

• shared_ptr의 리소스를 카운팅을 증가하지않은채 가지고 있는 포인터.

• 레퍼런스 카운팅에 영향을 주지 않는다.

• Shared_ptr 객체를 생성후 사용해야 한다.

Page 29: Cpp 0x kimRyungee

Smart Pointer -unique_ptr-

• 할당된 객체를 공유하지 않는 스마트 포인터.

• Move 생성자/연산자만 사용할 수 있다.

Page 30: Cpp 0x kimRyungee

참고자료

• http://msdn.microsoft.com/en-us/library/cscc687y.aspx

• http://herbsutter.com

• http://en.wikipedia.org/

• http://www.open-std.org/JTC1/SC22/WG21/

• Effective STL

Page 31: Cpp 0x kimRyungee