SlideShare a Scribd company logo
CHAPTER 1
EFFECTIVE MODERN C++
김지환

mastrayer@naver.com
CHAPTER 1. 형식 연역
ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라
auto는 한 가지 예외를 제외하고는 템플릿 타입 추론과 동일한 규칙
을 가진다. (item 1의 내용)

auto = 템플릿의 T

타입 지정자 = ParamType 이라고 볼 수 있다.
CHAPTER 1. 형식 연역
ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라
auto x = 27;
const auto cx = x;
const auto& rx = x;
1. x의 타입 지정자는 auto 그 자체이다.

2. cx의 형식 지정자는 const auto이다.

3. rx의 형식 지정자는 const auto&이다.
CHAPTER 1. 형식 연역
ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라
auto x = 27; // case3 - x는 포인터도 참조도 아님
const auto cx = x; // case3
const auto& rx = x; // case1 - rx는 보편참조(universal reference)가 아닌 참조
auto&& uref1 = x; // x: int(lvalue) , uref1: int&
auto&& uref2 = cx; // cx: const int(lvalue) , uref2: const int&
auto&& uref3 = 27; // 27: int(rvalue) , uref3: int&&
// array decays into a pointer
const char name[]
= "R. N. Briggs"; // const char[13]
auto arr1 = name; // const char*
auto& arr2 = name; // const char (&)[13]
void someFunc(int, double);
auto func1 = someFunc; // void (*)(int, double)
auto& func2 = someFunc; // void (&)(int, double)
CHAPTER 1. 형식 연역
ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라
27을 초기값으로 하는 int를 선언하는 4가
지 구문이다.







이 구문들을 auto로 사용하면 어떻게 될까?
int x1 = 27; // C++98
int x2(27); // C++98
int x3 = { 27 }; // C++11
int x4{ 27 }; // C++11
CHAPTER 1. 형식 연역
ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라
auto x1 = 27; // int, 27
auto x2(27); // int, 27
auto x3 = { 27 }; // std::initializer_list<int>, { 27 }
auto x4{ 27 }; // std::initializer_list<int>, { 27 }



auto x5 = { 1, 2, 3.0 }; // ERROR!
// std::initializer_list<T>의 T를 타입 추론할 수 없다.
1. 똑같은 결과를 가지는 4가지 스타일의 구문이 auto에선 다른 타입으로 추론되었다.

2. 중괄호 쌍으로 감싸인 형태(C++11 uniform initialization)는 std::initializer_list<T>로 타입 추론된다.
CHAPTER 1. 형식 연역
ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라
auto createInitList() {
return { 1, 2, 3 }; // ERROR!
}
std::vector<int> v;
auto resetV =
[&][const auto& newValue) { v = newValue; }; // C++ 14 lambda
resetV({ 1, 2, 3 }); // ERROR!
1. createInitList의 반환값 {1,2,3}을 타입 추론할 수 없기 때문에 에러가 난다.

2. C++ 14의 람다에서도 마찬가지로 resetV에 넘겨준 {1,2,3}의 타입을 추론할 수 없기 때문에 마찬가지로 에러.

3. 이러한 auto의 용법들은 auto의 타입 추론이 아니라 템플릿 형식 연역의 규칙들이 적용된다.

More Related Content

What's hot

[SwiftStudy 2016] 3장. 함수
[SwiftStudy 2016] 3장. 함수[SwiftStudy 2016] 3장. 함수
[SwiftStudy 2016] 3장. 함수
Keunhyun Oh
 
[C++ korea] effective modern c++ study item 1정은식
[C++ korea] effective modern c++ study item 1정은식[C++ korea] effective modern c++ study item 1정은식
[C++ korea] effective modern c++ study item 1정은식
은식 정
 
[C++ korea] effective modern c++ study item 1 understand template type dedu...
[C++ korea] effective modern c++ study   item 1 understand template type dedu...[C++ korea] effective modern c++ study   item 1 understand template type dedu...
[C++ korea] effective modern c++ study item 1 understand template type dedu...
Seok-joon Yun
 
데이터베이스 정규화
데이터베이스 정규화데이터베이스 정규화
데이터베이스 정규화
Hoyoung Jung
 
[SwiftStudy 2016] 2장. Swift 타입 파트 1
[SwiftStudy 2016] 2장. Swift 타입 파트 1[SwiftStudy 2016] 2장. Swift 타입 파트 1
[SwiftStudy 2016] 2장. Swift 타입 파트 1
Keunhyun Oh
 
[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
 
Java generics
Java genericsJava generics
Java generics
Jaesup Kwak
 
모어 이펙티브 c++ 1,2장 스터디
모어 이펙티브 c++ 1,2장 스터디모어 이펙티브 c++ 1,2장 스터디
모어 이펙티브 c++ 1,2장 스터디
quxn6
 
Ruby - 6th (루비 6장 변수와 식)
Ruby - 6th (루비 6장 변수와 식)Ruby - 6th (루비 6장 변수와 식)
Ruby - 6th (루비 6장 변수와 식)재영 이
 
More effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshinMore effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshin
Dong Chan Shin
 
effective c++ chapter 3~4 정리
effective c++ chapter 3~4 정리effective c++ chapter 3~4 정리
effective c++ chapter 3~4 정리
Injae Lee
 
Effective c++ 4
Effective c++ 4Effective c++ 4
Effective c++ 4현찬 양
 
Secure coding-c-dcl-1
Secure coding-c-dcl-1Secure coding-c-dcl-1
Secure coding-c-dcl-1
Seungyong Lee
 
More effective c++ 2
More effective c++ 2More effective c++ 2
More effective c++ 2현찬 양
 
More effective c++ 1
More effective c++ 1More effective c++ 1
More effective c++ 1현찬 양
 
More effective c++ 항목30부터
More effective c++ 항목30부터More effective c++ 항목30부터
More effective c++ 항목30부터
Dong Chan Shin
 
Start IoT with JavaScript - 4.객체1
Start IoT with JavaScript - 4.객체1Start IoT with JavaScript - 4.객체1
Start IoT with JavaScript - 4.객체1
Park Jonggun
 
Start IoT with JavaScript - 2.연산자
Start IoT with JavaScript - 2.연산자Start IoT with JavaScript - 2.연산자
Start IoT with JavaScript - 2.연산자
Park Jonggun
 
Effective c++ 1
Effective c++ 1Effective c++ 1
Effective c++ 1현찬 양
 

What's hot (20)

[SwiftStudy 2016] 3장. 함수
[SwiftStudy 2016] 3장. 함수[SwiftStudy 2016] 3장. 함수
[SwiftStudy 2016] 3장. 함수
 
[C++ korea] effective modern c++ study item 1정은식
[C++ korea] effective modern c++ study item 1정은식[C++ korea] effective modern c++ study item 1정은식
[C++ korea] effective modern c++ study item 1정은식
 
[C++ korea] effective modern c++ study item 1 understand template type dedu...
[C++ korea] effective modern c++ study   item 1 understand template type dedu...[C++ korea] effective modern c++ study   item 1 understand template type dedu...
[C++ korea] effective modern c++ study item 1 understand template type dedu...
 
데이터베이스 정규화
데이터베이스 정규화데이터베이스 정규화
데이터베이스 정규화
 
[SwiftStudy 2016] 2장. Swift 타입 파트 1
[SwiftStudy 2016] 2장. Swift 타입 파트 1[SwiftStudy 2016] 2장. Swift 타입 파트 1
[SwiftStudy 2016] 2장. Swift 타입 파트 1
 
[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...
 
Java generics
Java genericsJava generics
Java generics
 
모어 이펙티브 c++ 1,2장 스터디
모어 이펙티브 c++ 1,2장 스터디모어 이펙티브 c++ 1,2장 스터디
모어 이펙티브 c++ 1,2장 스터디
 
Ruby - 6th (루비 6장 변수와 식)
Ruby - 6th (루비 6장 변수와 식)Ruby - 6th (루비 6장 변수와 식)
Ruby - 6th (루비 6장 변수와 식)
 
More effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshinMore effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshin
 
effective c++ chapter 3~4 정리
effective c++ chapter 3~4 정리effective c++ chapter 3~4 정리
effective c++ chapter 3~4 정리
 
Effective c++ 4
Effective c++ 4Effective c++ 4
Effective c++ 4
 
Secure coding-c-dcl-1
Secure coding-c-dcl-1Secure coding-c-dcl-1
Secure coding-c-dcl-1
 
More effective c++ 2
More effective c++ 2More effective c++ 2
More effective c++ 2
 
More effective c++ 1
More effective c++ 1More effective c++ 1
More effective c++ 1
 
More effective c++ 항목30부터
More effective c++ 항목30부터More effective c++ 항목30부터
More effective c++ 항목30부터
 
Start IoT with JavaScript - 4.객체1
Start IoT with JavaScript - 4.객체1Start IoT with JavaScript - 4.객체1
Start IoT with JavaScript - 4.객체1
 
5 6 1
5 6 15 6 1
5 6 1
 
Start IoT with JavaScript - 2.연산자
Start IoT with JavaScript - 2.연산자Start IoT with JavaScript - 2.연산자
Start IoT with JavaScript - 2.연산자
 
Effective c++ 1
Effective c++ 1Effective c++ 1
Effective c++ 1
 

Viewers also liked

C#으로 게임 엔진 만들기(3)
C#으로 게임 엔진 만들기(3)C#으로 게임 엔진 만들기(3)
C#으로 게임 엔진 만들기(3)
지환 김
 
Json for modern c++
Json for modern c++Json for modern c++
Json for modern c++
지환 김
 
C#으로 게임 엔진 만들기(2)
C#으로 게임 엔진 만들기(2)C#으로 게임 엔진 만들기(2)
C#으로 게임 엔진 만들기(2)지환 김
 
C#으로 게임 엔진 만들기(1)
C#으로 게임 엔진 만들기(1)C#으로 게임 엔진 만들기(1)
C#으로 게임 엔진 만들기(1)지환 김
 
Easyloggingpp
EasyloggingppEasyloggingpp
Easyloggingpp
지환 김
 
Modernizing
ModernizingModernizing
Modernizing
승훈 오
 
Hierarchical prediction and context adaptive coding for lossless color image ...
Hierarchical prediction and context adaptive coding for lossless color image ...Hierarchical prediction and context adaptive coding for lossless color image ...
Hierarchical prediction and context adaptive coding for lossless color image ...
LeMeniz Infotech
 
The hands
The handsThe hands
The hands
english-kostroma
 
Chikmagalur
ChikmagalurChikmagalur
Ten Modern Plagues - for Seder Discussion
Ten Modern Plagues - for Seder DiscussionTen Modern Plagues - for Seder Discussion
Ten Modern Plagues - for Seder Discussion
Arnie Fertig
 
Dominating set and network coding based routing in wireless mesh netwoks
Dominating set and network coding based routing in wireless mesh netwoksDominating set and network coding based routing in wireless mesh netwoks
Dominating set and network coding based routing in wireless mesh netwoks
LeMeniz Infotech
 
Lion opportunities plan BY AL AMIN
Lion opportunities plan BY  AL AMINLion opportunities plan BY  AL AMIN
Lion opportunities plan BY AL AMIN
Alamin Pathan
 
My ppt game carlos suarez
My ppt game carlos suarezMy ppt game carlos suarez
My ppt game carlos suarez
carlos27748318
 
Cooperative caching for efficient data access in disruption tolerant networks
Cooperative caching for efficient data access in disruption tolerant networksCooperative caching for efficient data access in disruption tolerant networks
Cooperative caching for efficient data access in disruption tolerant networks
LeMeniz Infotech
 
Trusted db a trusted hardware based database with privacy and data confidenti...
Trusted db a trusted hardware based database with privacy and data confidenti...Trusted db a trusted hardware based database with privacy and data confidenti...
Trusted db a trusted hardware based database with privacy and data confidenti...
LeMeniz Infotech
 
Консультант по карьере
Консультант по карьере Консультант по карьере
Консультант по карьере
Yelena Shaulova
 
Retouched pictures
Retouched picturesRetouched pictures
Retouched pictures
mdrouet44
 
Presentación curso ABP
Presentación curso ABPPresentación curso ABP
Presentación curso ABP
MCarmenGbogarra
 
Naturaleza
NaturalezaNaturaleza
Naturaleza
diosyely
 

Viewers also liked (19)

C#으로 게임 엔진 만들기(3)
C#으로 게임 엔진 만들기(3)C#으로 게임 엔진 만들기(3)
C#으로 게임 엔진 만들기(3)
 
Json for modern c++
Json for modern c++Json for modern c++
Json for modern c++
 
C#으로 게임 엔진 만들기(2)
C#으로 게임 엔진 만들기(2)C#으로 게임 엔진 만들기(2)
C#으로 게임 엔진 만들기(2)
 
C#으로 게임 엔진 만들기(1)
C#으로 게임 엔진 만들기(1)C#으로 게임 엔진 만들기(1)
C#으로 게임 엔진 만들기(1)
 
Easyloggingpp
EasyloggingppEasyloggingpp
Easyloggingpp
 
Modernizing
ModernizingModernizing
Modernizing
 
Hierarchical prediction and context adaptive coding for lossless color image ...
Hierarchical prediction and context adaptive coding for lossless color image ...Hierarchical prediction and context adaptive coding for lossless color image ...
Hierarchical prediction and context adaptive coding for lossless color image ...
 
The hands
The handsThe hands
The hands
 
Chikmagalur
ChikmagalurChikmagalur
Chikmagalur
 
Ten Modern Plagues - for Seder Discussion
Ten Modern Plagues - for Seder DiscussionTen Modern Plagues - for Seder Discussion
Ten Modern Plagues - for Seder Discussion
 
Dominating set and network coding based routing in wireless mesh netwoks
Dominating set and network coding based routing in wireless mesh netwoksDominating set and network coding based routing in wireless mesh netwoks
Dominating set and network coding based routing in wireless mesh netwoks
 
Lion opportunities plan BY AL AMIN
Lion opportunities plan BY  AL AMINLion opportunities plan BY  AL AMIN
Lion opportunities plan BY AL AMIN
 
My ppt game carlos suarez
My ppt game carlos suarezMy ppt game carlos suarez
My ppt game carlos suarez
 
Cooperative caching for efficient data access in disruption tolerant networks
Cooperative caching for efficient data access in disruption tolerant networksCooperative caching for efficient data access in disruption tolerant networks
Cooperative caching for efficient data access in disruption tolerant networks
 
Trusted db a trusted hardware based database with privacy and data confidenti...
Trusted db a trusted hardware based database with privacy and data confidenti...Trusted db a trusted hardware based database with privacy and data confidenti...
Trusted db a trusted hardware based database with privacy and data confidenti...
 
Консультант по карьере
Консультант по карьере Консультант по карьере
Консультант по карьере
 
Retouched pictures
Retouched picturesRetouched pictures
Retouched pictures
 
Presentación curso ABP
Presentación curso ABPPresentación curso ABP
Presentación curso ABP
 
Naturaleza
NaturalezaNaturaleza
Naturaleza
 

Similar to [Effective Modern C++] Chapter1 - item2

Modern Effective C++ Item2 Understanding Auto type deducing
Modern Effective C++ Item2 Understanding Auto type deducingModern Effective C++ Item2 Understanding Auto type deducing
Modern Effective C++ Item2 Understanding Auto type deducing
건 손
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1
Chris Ohk
 
Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약
Nam Hyeonuk
 
C++ 타입 추론
C++ 타입 추론C++ 타입 추론
C++ 타입 추론
Huey Park
 
[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
Chris Ohk
 
Effective c++chapter4
Effective c++chapter4Effective c++chapter4
Effective c++chapter4
성연 김
 

Similar to [Effective Modern C++] Chapter1 - item2 (6)

Modern Effective C++ Item2 Understanding Auto type deducing
Modern Effective C++ Item2 Understanding Auto type deducingModern Effective C++ Item2 Understanding Auto type deducing
Modern Effective C++ Item2 Understanding Auto type deducing
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1
 
Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약
 
C++ 타입 추론
C++ 타입 추론C++ 타입 추론
C++ 타입 추론
 
[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
 
Effective c++chapter4
Effective c++chapter4Effective c++chapter4
Effective c++chapter4
 

[Effective Modern C++] Chapter1 - item2

  • 1. CHAPTER 1 EFFECTIVE MODERN C++ 김지환
 mastrayer@naver.com
  • 2. CHAPTER 1. 형식 연역 ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라 auto는 한 가지 예외를 제외하고는 템플릿 타입 추론과 동일한 규칙 을 가진다. (item 1의 내용) auto = 템플릿의 T
 타입 지정자 = ParamType 이라고 볼 수 있다.
  • 3. CHAPTER 1. 형식 연역 ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라 auto x = 27; const auto cx = x; const auto& rx = x; 1. x의 타입 지정자는 auto 그 자체이다. 2. cx의 형식 지정자는 const auto이다. 3. rx의 형식 지정자는 const auto&이다.
  • 4. CHAPTER 1. 형식 연역 ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라 auto x = 27; // case3 - x는 포인터도 참조도 아님 const auto cx = x; // case3 const auto& rx = x; // case1 - rx는 보편참조(universal reference)가 아닌 참조 auto&& uref1 = x; // x: int(lvalue) , uref1: int& auto&& uref2 = cx; // cx: const int(lvalue) , uref2: const int& auto&& uref3 = 27; // 27: int(rvalue) , uref3: int&& // array decays into a pointer const char name[] = "R. N. Briggs"; // const char[13] auto arr1 = name; // const char* auto& arr2 = name; // const char (&)[13] void someFunc(int, double); auto func1 = someFunc; // void (*)(int, double) auto& func2 = someFunc; // void (&)(int, double)
  • 5. CHAPTER 1. 형식 연역 ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라 27을 초기값으로 하는 int를 선언하는 4가 지 구문이다.
 
 
 
 이 구문들을 auto로 사용하면 어떻게 될까? int x1 = 27; // C++98 int x2(27); // C++98 int x3 = { 27 }; // C++11 int x4{ 27 }; // C++11
  • 6. CHAPTER 1. 형식 연역 ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라 auto x1 = 27; // int, 27 auto x2(27); // int, 27 auto x3 = { 27 }; // std::initializer_list<int>, { 27 } auto x4{ 27 }; // std::initializer_list<int>, { 27 }
 
 auto x5 = { 1, 2, 3.0 }; // ERROR! // std::initializer_list<T>의 T를 타입 추론할 수 없다. 1. 똑같은 결과를 가지는 4가지 스타일의 구문이 auto에선 다른 타입으로 추론되었다. 2. 중괄호 쌍으로 감싸인 형태(C++11 uniform initialization)는 std::initializer_list<T>로 타입 추론된다.
  • 7. CHAPTER 1. 형식 연역 ITEM 2 : AUTO의 형식 연역 규칙을 숙지하라 auto createInitList() { return { 1, 2, 3 }; // ERROR! } std::vector<int> v; auto resetV = [&][const auto& newValue) { v = newValue; }; // C++ 14 lambda resetV({ 1, 2, 3 }); // ERROR! 1. createInitList의 반환값 {1,2,3}을 타입 추론할 수 없기 때문에 에러가 난다. 2. C++ 14의 람다에서도 마찬가지로 resetV에 넘겨준 {1,2,3}의 타입을 추론할 수 없기 때문에 마찬가지로 에러. 3. 이러한 auto의 용법들은 auto의 타입 추론이 아니라 템플릿 형식 연역의 규칙들이 적용된다.