SlideShare a Scribd company logo
1 of 25
C++0x
2011.07.18.
메신저플랫폼개발팀
임유빈
auto
●대입하는 값 형태에 따라 컴파일러가 판단하여 변수타입을 정
의함.
●컴파일러가 판단해야하므로 변수는 반드시 초기화.
auto i = 10; // int
auto k; // error!
for ( auto ib = mystl.begin(), ie = mystl.end(); ib != ie; ib++
) { ... }
auto
●리턴 타입을 함수 마지막에 적는 형식
●auto function (parameters) -> returntype { ... }
●decltype과 함께 메타 프로그래밍 할 때 유용
auto function(int a) -> int { return a; }
int function(int a) { return a; }
auto function(int a, double b) -> decltype(a*b) { return a*b; }
decltype(a*b) function(int a, double b) { return a*b; } // error! -
a, b가 아직 나오지 않아 추론할 수 없음
decltype
●선언한 타입을 반환하는 것으로 메타 프로그래밍용.
●인자에 들어오는 것 어떠한 것도 실행하지 않음.
struct A { double x; };
const A a = { 1.0 };
const double b = 1.0;
decltype(a.x) ac = 1.0;
decltype(b) bc = 1.0;
ac = 3.0;
//bc = 3.0; - error `bc' is `const double'.
nullptr
●기존 매크로 NULL은 integer과 구분할 수 없음.
●nullptr는 정확히 주소 '0'를 가르키는 포인터.
static_assert
●컴파일 타임에 조건을 검사하여 오류 발생.
●조건식은 반드시 상수 비교
#define TEST 1
...
static_assert(TEST==0, "Error!"); // compile error!
lamda
●이름 없는 함수
●STL류에서 Functor를 만들 때, 편리
●형식: [captures] (parameters) -> returntype { /* implement */ } ();
o[] { return 3.14; } (); // 실행
oauto functor_1 = [] (int a) -> int { return a; }; // 구현 및 대입
int iv[] = { 2, 9, 3, 1, 7, 6 };
sort(iv, iv+6, [] (int a, int b) -> bool{ return a < b; });
for_each(iv, iv+6, [] (int a) { cout << a << endl; } );
using
●이름 재정의 - typedef와 다름
●typedef는 타입에 대한 alias를 추가하는 것이지 새로운 타입을
추가하는 것이 아님 - 약한 형식 검사
●범위를 정할 수 있음.
using NewName = OldName;
using Namespace::Name; == using Name = Namespace::Name;
int i; using r = i; == int i; int& r = i;
using Cos = std::cos; != using C = std::cos(double);
enum
●enum에 타입을 줄 수 있음. (기본형 int)
●enum의 변수의 범위 지정 가능. (기본은 글로벌)
oenum class Name : Type { enums... };
enum class E : unsigned int { E1, E2, E3 };
enum class F : unsigned int { E1, E2, E3 };
int
main(int,char*[])
{
E e = E::E1;
F f = F::E1;
}
friend
●typedef 로 정의한 타입도 추가 가능
●템플릿 인수 사용 가능
class C;
typedef Ctype C;
template<typename _T>
class X1 {
friend class C;
friend class Ctype;
friend class _T;
};
for
●범위 기반 반복
int iv[] = { 1, 2, 9, 3, 6 };
for ( int& i : iv ) cout << i << endl;
vector<int> isv = { 1, 2, 9, 3, 6 };
for ( int i : isv ) cout << i << endl;
>>
●기존에는 '>>'를 무조건 우측 시프트 연산자로 인식
●Nested Template 사용 시 띄어 쓰지 않아도 됨
#include <vector>
using namespace std;
typedef vector<vector<int>> double_vector;
delegating constructor
●같은 클래스 내에 다른 생성자에게 위임
class MyClass {
public:
MyClass(int a, int b) { ... }
MyClass() : MyClass(0, 0) { ... }
MyClass(const MyClass& obj) : MyClass(obj.a, obj.b) { ... }
};
inheriting constructor
●부모 클래스 생성자를 암묵적으로 상속.
●클래스 내 using Parent::Parent; 구문을 사용하여 상속.
●using Parent::Parent(...); 구문으로 일부 상속 가능.
●자식 멤버 변수 초기화는 하지 않으니 주의.
●기본생성자와 복사생성자는 상속하지 않으니 주의.
class Parent { public: Parent(int a) { ... } };
class Child {
public:
using Parent::Parent;
int a;
};
constexpr
●수식 또는 함수가 상수 표현식인 것을 명시하여, 상수만을 필
요로 하는 문법에 사용할 수 있도록 함.
constexpr int size(void) { return 1+5; }
int v[size()];
std::initializer_list
●초기화 목록
void func(intializer_list<int> s) { for ( int& x : s ) cout << x <<
endl; }
int main(int, char*[]) {
func({1,2,3,4});
vector<int> v = { 1,2,3,4,5};
}
extern templates
●명시적으로 템플릿을 인스턴스화 하여 빌드 시간 단축 및 실행
파일크기 단축
●a.h
template<typename T>
class AType { ... };
●a.cpp
#include "a.h"
void func(void) { AType<int> inst; ... }
●main.cpp
#include "a.h"
extern template AType<int>;
int main(int,char*[]) { AType<int> inst; }
in-class member initializers
●정적 상수 멤버 변수에 한해서 클래스 선언문 안에서 초기화
class AType
{
static const int s1 = 20;
};
rvalue references
●우측값 레퍼런스
●암묵적으로 생성되는 임시변수 처리용
class RValue {
RValue(RValue&& in) : buf(in.buf) { in.buf = nullptr; }
RValue& operator = (RValue&& in) { buf = in.buf; in.buf =
nullptr; }
};
RValue r;
r = RValue(...);
preventing narrowing
●암묵적 형변환으로 인한 데이터 유실을 허락하지 않음
int x = 7.3; // 암묵적 형변환
int y{ 7.3 }; // 데이터 유실 발생이 확실하므로 컴파일 타임에
오류 발생
override contorls: final
●더 이상 오버라이드 하지 않음
class Parent {
virtual void func1() final { ... }
};
class Child {
virtual void func1() { ... } // error!
};
threads and atomic operations
●thread_local
●std::thread
●std::mutex, std::recursive_mutex
●std::condition_variable, std::condition_variable_any
●std::lock_guard, std::unique_lock
regular expressions
●표준 정규식 지원
std::regex rgx("[ ,.tn;:]");
std::cmatch match;
const char* target = "Unseen University - Ankh-Morpork";
if ( std::regex_search(target, match, rgx) ) {
const size_t n = match.size();
for ( size_t a = 0; a < n; a++ ) {
cout.write(match[a].first, match[a].second) << endl;
}
}
general-purpose smart pointers
●std::shared_ptr 지원
References...
●http://en.wikipedia.org/wiki/C%2B%2B0x
●http://occamsrazr.net/tt/tag/C++0x
●http://frompt.egloos.com/category/Story%20Of%20C%2B%2B
●http://www2.research.att.com/~bs/C++0xFAQ.html

More Related Content

What's hot

[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준Seok-joon Yun
 
Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심흥배 최
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기Jongwook Choi
 
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3Chris Ohk
 
[C++ korea] effective modern c++ study item 2 understanding auto type deduc...
[C++ korea] effective modern c++ study   item 2 understanding auto type deduc...[C++ korea] effective modern c++ study   item 2 understanding auto type deduc...
[C++ korea] effective modern c++ study item 2 understanding auto type deduc...Seok-joon Yun
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features SummaryChris Ohk
 
C#을 사용한 빠른 툴 개발
C#을 사용한 빠른 툴 개발C#을 사용한 빠른 툴 개발
C#을 사용한 빠른 툴 개발흥배 최
 
Visual studio 2010
Visual studio 2010Visual studio 2010
Visual studio 2010MinGeun Park
 
[C++ korea] Effective Modern C++ 신촌 Study Item20,21,23
[C++ korea] Effective Modern C++ 신촌 Study Item20,21,23[C++ korea] Effective Modern C++ 신촌 Study Item20,21,23
[C++ korea] Effective Modern C++ 신촌 Study Item20,21,23Seok-joon Yun
 
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...Seok-joon Yun
 
C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2Chris Ohk
 
C++ 타입 추론
C++ 타입 추론C++ 타입 추론
C++ 타입 추론Huey Park
 
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...Seok-joon Yun
 
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...Seok-joon Yun
 
Template at c++
Template at c++Template at c++
Template at c++Lusain Kim
 
Pure Function and Honest Design
Pure Function and Honest DesignPure Function and Honest Design
Pure Function and Honest DesignHyungho Ko
 
C++’s move semantics
C++’s move semanticsC++’s move semantics
C++’s move semanticsLusain Kim
 
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...Seok-joon Yun
 
Pure Function and Rx
Pure Function and RxPure Function and Rx
Pure Function and RxHyungho Ko
 

What's hot (20)

[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
 
Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기
 
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
 
[C++ korea] effective modern c++ study item 2 understanding auto type deduc...
[C++ korea] effective modern c++ study   item 2 understanding auto type deduc...[C++ korea] effective modern c++ study   item 2 understanding auto type deduc...
[C++ korea] effective modern c++ study item 2 understanding auto type deduc...
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary
 
C++11
C++11C++11
C++11
 
C#을 사용한 빠른 툴 개발
C#을 사용한 빠른 툴 개발C#을 사용한 빠른 툴 개발
C#을 사용한 빠른 툴 개발
 
Visual studio 2010
Visual studio 2010Visual studio 2010
Visual studio 2010
 
[C++ korea] Effective Modern C++ 신촌 Study Item20,21,23
[C++ korea] Effective Modern C++ 신촌 Study Item20,21,23[C++ korea] Effective Modern C++ 신촌 Study Item20,21,23
[C++ korea] Effective Modern C++ 신촌 Study Item20,21,23
 
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...
 
C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2
 
C++ 타입 추론
C++ 타입 추론C++ 타입 추론
C++ 타입 추론
 
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
 
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
 
Template at c++
Template at c++Template at c++
Template at c++
 
Pure Function and Honest Design
Pure Function and Honest DesignPure Function and Honest Design
Pure Function and Honest Design
 
C++’s move semantics
C++’s move semanticsC++’s move semantics
C++’s move semantics
 
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
 
Pure Function and Rx
Pure Function and RxPure Function and Rx
Pure Function and Rx
 

Viewers also liked

20150212 c++11 features used in crow
20150212 c++11 features used in crow20150212 c++11 features used in crow
20150212 c++11 features used in crowJaeseung Ha
 
초보를 위한 C++11
초보를 위한 C++11초보를 위한 C++11
초보를 위한 C++11Minhyuk Kwon
 
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로Jaeseung Ha
 
C++ 11 에 대해서 쉽게 알아봅시다 1부
C++ 11 에 대해서 쉽게 알아봅시다 1부C++ 11 에 대해서 쉽게 알아봅시다 1부
C++ 11 에 대해서 쉽게 알아봅시다 1부Gwangwhi Mah
 
[1116 박민근] c++11에 추가된 새로운 기능들
[1116 박민근] c++11에 추가된 새로운 기능들[1116 박민근] c++11에 추가된 새로운 기능들
[1116 박민근] c++11에 추가된 새로운 기능들MinGeun Park
 
Boost 라이브리와 C++11
Boost 라이브리와 C++11Boost 라이브리와 C++11
Boost 라이브리와 C++11OnGameServer
 
C++11에서 주의해야할 것들
C++11에서 주의해야할 것들C++11에서 주의해야할 것들
C++11에서 주의해야할 것들Sangwook Kwon
 

Viewers also liked (11)

c++11
c++11c++11
c++11
 
C++11(최지웅)
C++11(최지웅)C++11(최지웅)
C++11(최지웅)
 
[C++ adv] c++11
[C++ adv] c++11[C++ adv] c++11
[C++ adv] c++11
 
20150212 c++11 features used in crow
20150212 c++11 features used in crow20150212 c++11 features used in crow
20150212 c++11 features used in crow
 
초보를 위한 C++11
초보를 위한 C++11초보를 위한 C++11
초보를 위한 C++11
 
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
 
C++ 11 에 대해서 쉽게 알아봅시다 1부
C++ 11 에 대해서 쉽게 알아봅시다 1부C++ 11 에 대해서 쉽게 알아봅시다 1부
C++ 11 에 대해서 쉽게 알아봅시다 1부
 
[1116 박민근] c++11에 추가된 새로운 기능들
[1116 박민근] c++11에 추가된 새로운 기능들[1116 박민근] c++11에 추가된 새로운 기능들
[1116 박민근] c++11에 추가된 새로운 기능들
 
Boost 라이브리와 C++11
Boost 라이브리와 C++11Boost 라이브리와 C++11
Boost 라이브리와 C++11
 
C++11에서 주의해야할 것들
C++11에서 주의해야할 것들C++11에서 주의해야할 것들
C++11에서 주의해야할 것들
 
C++11
C++11C++11
C++11
 

Similar to C++11

프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2dktm
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉HyunJoon Park
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1Chris Ohk
 
포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++KWANGIL KIM
 
14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿유석 남
 
가능한 C++ 스타일의 캐스트를 즐겨 쓰자
가능한 C++ 스타일의 캐스트를 즐겨 쓰자가능한 C++ 스타일의 캐스트를 즐겨 쓰자
가능한 C++ 스타일의 캐스트를 즐겨 쓰자민욱 이
 
Gpg gems1 1.3
Gpg gems1 1.3Gpg gems1 1.3
Gpg gems1 1.3david nc
 
학교에서 배우지 않는 C
학교에서 배우지 않는 C학교에서 배우지 않는 C
학교에서 배우지 않는 CHeesuk Kang
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 명신 김
 
Let's Go (golang)
Let's Go (golang)Let's Go (golang)
Let's Go (golang)상욱 송
 
C수업자료
C수업자료C수업자료
C수업자료koominsu
 
C수업자료
C수업자료C수업자료
C수업자료koominsu
 
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)Sang Don Kim
 
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기Chris Ohk
 

Similar to C++11 (20)

C review
C  reviewC  review
C review
 
06장 함수
06장 함수06장 함수
06장 함수
 
프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2
 
6 function
6 function6 function
6 function
 
HI-ARC PS 101
HI-ARC PS 101HI-ARC PS 101
HI-ARC PS 101
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1
 
3.포인터
3.포인터3.포인터
3.포인터
 
포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++
 
java_2장.pptx
java_2장.pptxjava_2장.pptx
java_2장.pptx
 
14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿
 
가능한 C++ 스타일의 캐스트를 즐겨 쓰자
가능한 C++ 스타일의 캐스트를 즐겨 쓰자가능한 C++ 스타일의 캐스트를 즐겨 쓰자
가능한 C++ 스타일의 캐스트를 즐겨 쓰자
 
Gpg gems1 1.3
Gpg gems1 1.3Gpg gems1 1.3
Gpg gems1 1.3
 
학교에서 배우지 않는 C
학교에서 배우지 않는 C학교에서 배우지 않는 C
학교에서 배우지 않는 C
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14
 
Let's Go (golang)
Let's Go (golang)Let's Go (golang)
Let's Go (golang)
 
C수업자료
C수업자료C수업자료
C수업자료
 
C수업자료
C수업자료C수업자료
C수업자료
 
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
 
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
 

C++11

  • 2. auto ●대입하는 값 형태에 따라 컴파일러가 판단하여 변수타입을 정 의함. ●컴파일러가 판단해야하므로 변수는 반드시 초기화. auto i = 10; // int auto k; // error! for ( auto ib = mystl.begin(), ie = mystl.end(); ib != ie; ib++ ) { ... }
  • 3. auto ●리턴 타입을 함수 마지막에 적는 형식 ●auto function (parameters) -> returntype { ... } ●decltype과 함께 메타 프로그래밍 할 때 유용 auto function(int a) -> int { return a; } int function(int a) { return a; } auto function(int a, double b) -> decltype(a*b) { return a*b; } decltype(a*b) function(int a, double b) { return a*b; } // error! - a, b가 아직 나오지 않아 추론할 수 없음
  • 4. decltype ●선언한 타입을 반환하는 것으로 메타 프로그래밍용. ●인자에 들어오는 것 어떠한 것도 실행하지 않음. struct A { double x; }; const A a = { 1.0 }; const double b = 1.0; decltype(a.x) ac = 1.0; decltype(b) bc = 1.0; ac = 3.0; //bc = 3.0; - error `bc' is `const double'.
  • 5. nullptr ●기존 매크로 NULL은 integer과 구분할 수 없음. ●nullptr는 정확히 주소 '0'를 가르키는 포인터.
  • 6. static_assert ●컴파일 타임에 조건을 검사하여 오류 발생. ●조건식은 반드시 상수 비교 #define TEST 1 ... static_assert(TEST==0, "Error!"); // compile error!
  • 7. lamda ●이름 없는 함수 ●STL류에서 Functor를 만들 때, 편리 ●형식: [captures] (parameters) -> returntype { /* implement */ } (); o[] { return 3.14; } (); // 실행 oauto functor_1 = [] (int a) -> int { return a; }; // 구현 및 대입 int iv[] = { 2, 9, 3, 1, 7, 6 }; sort(iv, iv+6, [] (int a, int b) -> bool{ return a < b; }); for_each(iv, iv+6, [] (int a) { cout << a << endl; } );
  • 8. using ●이름 재정의 - typedef와 다름 ●typedef는 타입에 대한 alias를 추가하는 것이지 새로운 타입을 추가하는 것이 아님 - 약한 형식 검사 ●범위를 정할 수 있음. using NewName = OldName; using Namespace::Name; == using Name = Namespace::Name; int i; using r = i; == int i; int& r = i; using Cos = std::cos; != using C = std::cos(double);
  • 9. enum ●enum에 타입을 줄 수 있음. (기본형 int) ●enum의 변수의 범위 지정 가능. (기본은 글로벌) oenum class Name : Type { enums... }; enum class E : unsigned int { E1, E2, E3 }; enum class F : unsigned int { E1, E2, E3 }; int main(int,char*[]) { E e = E::E1; F f = F::E1; }
  • 10. friend ●typedef 로 정의한 타입도 추가 가능 ●템플릿 인수 사용 가능 class C; typedef Ctype C; template<typename _T> class X1 { friend class C; friend class Ctype; friend class _T; };
  • 11. for ●범위 기반 반복 int iv[] = { 1, 2, 9, 3, 6 }; for ( int& i : iv ) cout << i << endl; vector<int> isv = { 1, 2, 9, 3, 6 }; for ( int i : isv ) cout << i << endl;
  • 12. >> ●기존에는 '>>'를 무조건 우측 시프트 연산자로 인식 ●Nested Template 사용 시 띄어 쓰지 않아도 됨 #include <vector> using namespace std; typedef vector<vector<int>> double_vector;
  • 13. delegating constructor ●같은 클래스 내에 다른 생성자에게 위임 class MyClass { public: MyClass(int a, int b) { ... } MyClass() : MyClass(0, 0) { ... } MyClass(const MyClass& obj) : MyClass(obj.a, obj.b) { ... } };
  • 14. inheriting constructor ●부모 클래스 생성자를 암묵적으로 상속. ●클래스 내 using Parent::Parent; 구문을 사용하여 상속. ●using Parent::Parent(...); 구문으로 일부 상속 가능. ●자식 멤버 변수 초기화는 하지 않으니 주의. ●기본생성자와 복사생성자는 상속하지 않으니 주의. class Parent { public: Parent(int a) { ... } }; class Child { public: using Parent::Parent; int a; };
  • 15. constexpr ●수식 또는 함수가 상수 표현식인 것을 명시하여, 상수만을 필 요로 하는 문법에 사용할 수 있도록 함. constexpr int size(void) { return 1+5; } int v[size()];
  • 16. std::initializer_list ●초기화 목록 void func(intializer_list<int> s) { for ( int& x : s ) cout << x << endl; } int main(int, char*[]) { func({1,2,3,4}); vector<int> v = { 1,2,3,4,5}; }
  • 17. extern templates ●명시적으로 템플릿을 인스턴스화 하여 빌드 시간 단축 및 실행 파일크기 단축 ●a.h template<typename T> class AType { ... }; ●a.cpp #include "a.h" void func(void) { AType<int> inst; ... } ●main.cpp #include "a.h" extern template AType<int>; int main(int,char*[]) { AType<int> inst; }
  • 18. in-class member initializers ●정적 상수 멤버 변수에 한해서 클래스 선언문 안에서 초기화 class AType { static const int s1 = 20; };
  • 19. rvalue references ●우측값 레퍼런스 ●암묵적으로 생성되는 임시변수 처리용 class RValue { RValue(RValue&& in) : buf(in.buf) { in.buf = nullptr; } RValue& operator = (RValue&& in) { buf = in.buf; in.buf = nullptr; } }; RValue r; r = RValue(...);
  • 20. preventing narrowing ●암묵적 형변환으로 인한 데이터 유실을 허락하지 않음 int x = 7.3; // 암묵적 형변환 int y{ 7.3 }; // 데이터 유실 발생이 확실하므로 컴파일 타임에 오류 발생
  • 21. override contorls: final ●더 이상 오버라이드 하지 않음 class Parent { virtual void func1() final { ... } }; class Child { virtual void func1() { ... } // error! };
  • 22. threads and atomic operations ●thread_local ●std::thread ●std::mutex, std::recursive_mutex ●std::condition_variable, std::condition_variable_any ●std::lock_guard, std::unique_lock
  • 23. regular expressions ●표준 정규식 지원 std::regex rgx("[ ,.tn;:]"); std::cmatch match; const char* target = "Unseen University - Ankh-Morpork"; if ( std::regex_search(target, match, rgx) ) { const size_t n = match.size(); for ( size_t a = 0; a < n; a++ ) { cout.write(match[a].first, match[a].second) << endl; } }