cpp 0x kimryungee
DESCRIPTION
TRANSCRIPT
C++ 0x 달려 BOA요~
아.꿈.사 비밀 모임
발표 : 김연기
발표자 는 뉴규?
김연기아.꿈.사 오후반 스터디 그룹 장소 예약 담당(Pattern Oriented Software Architecture 2)
2008. 10 ~ Microsoft Visual C++ MVP
유콘시스템 Sw개발팀 지상관제 장비 SW 개발잉카 인터넷 보안개발팀 업데이트 모듈 개발.
http://twitter.com/scor7910http://scor7910.tistory.com
차례
• 새롭게 추가 된 것들.
– Lambda
– R-Value Reference
– auto, decltype, constexpr
– Smart Pointer
• 참고자료.
• Q&A
Lambda
• 명명되지 않은(Unnamed) 함수 객체.
Lambda
• 명명되지 않은(Unnamed) 함수 객체.
Lambda Introducer
Lambda
• 명명되지 않은(Unnamed) 함수 객체.
Lambda Parameter declaration
Lambda
• 명명되지 않은(Unnamed) 함수 객체.
Lambda Compound Statement
Lambda
• 명명되지 않은(Unnamed) 함수 객체.
Lambda Return Type
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;}
Lambda –함수객체-
struct LambdaFunctor {void operator()(int n) const {
cout << n << " ";}
};
…….
for_each(v.begin(), v.end(), LambdaFunctor() );
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;}
});
Lambda –캡쳐-
• 상위 스코프({…})의 변수를 람다 구현내부에서 사용할 수 있다.
• [변수1, 변수2] : 변수1, 변수2 캡쳐
• [&변수1, &변수2] : 변수1, 변수2 참조캡쳐
• [&] : 상위 스코프의 변수를 참조 캡쳐.
• [=] : 상위 스코프의 변수를 값 캡쳐.
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;
});
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;
});
R-Value Reference
• L-Value & R-Value
res = (++a
+ b++);
a=10;
b =13;
R-Value Reference
• L-Value & R-Value
res = (++a
+ b++);
a=10;
b =13;
ab
res++a
1013
b++(++a + b++)
R-Value Reference
• L-Value & R-Value
res = (++a
+ b++);
a=10;
b =13;
ab
res++a
1013
b++(++a + b++)
R-Value Reference
double& rd1 = 2.0; const double& rd2 = 2.0;double&& rd3 = 2.0;
rd2 += 1;rd3 +=3;
R-Value Reference
double& rd1 = 2.0; ERROR!! const double& rd2 = 2.0;double&& rd3 = 2.0;
rd2 += 1; ERROR!!rd3 +=3;
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;
}
auto
for (vector<int>::const_iterator itr = myvec.begin(); itr != myvec.end(); ++itr)
for (auto itr = myvec.begin(); itr != myvec.end(); ++itr)
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&
constexpr
constexptr GetBufferSize();
TCHAR buffer[GetBufferSize ()+ 12];
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;}
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;}
Smart Pointer –auto_ptr-
template<class _Other>_Myt& operator=(auto_ptr<_Other>& _Right) _THROW0(){
// assign compatible _Right (assume pointer)reset(_Right.release());return (*this);
}
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;
}
Smart Pointer -weak_ptr-
• shared_ptr의 리소스를 카운팅을 증가하지않은채 가지고 있는 포인터.
• 레퍼런스 카운팅에 영향을 주지 않는다.
• Shared_ptr 객체를 생성후 사용해야 한다.
Smart Pointer -unique_ptr-
• 할당된 객체를 공유하지 않는 스마트 포인터.
• Move 생성자/연산자만 사용할 수 있다.
참고자료
• 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