SlideShare a Scribd company logo
1 of 20
C++ 타입 추론
클라이언트 프로그래머
박재완
template, auto, [](){};
조금만 알아보고 시작합시다
Value C++ 11 이전
lvalue : 표현식 이후에도 지속되는 값
rvalue : 표현식 종료 이후에는 더이상
존재하지 않는 값
int Function();
int x = 3;
int y = Function();
조금만 알아보고 시작합시다
Value C++ 11 이후
lvalue : C++ 11 이전의 lvalue
prvalue : C++ 11 이전의 rvlaue
xvalue : 생명주기가 도래해가는 값,
rvlaue reference와 함께 등장,
"eXpiring value"의 약자
rvalue : xvalue + prvalue
glvalue : lvlaue + xvalue
조금만 알아보고 시작합시다
Universal Reference
template<typename T>
void f(T&& param)
auto&& x;
x는 rvalue reference가 아닙니다.
이제부터 타입 추론
C++ 98
template
C++ 11
auto, universal reference,
lambda capture and return, decltype
C++ 14
lambda init captures, function return
왜 알아야 하는가?
class Button
{
virtual void OnClicked();
}
class Button2 : public Button
{
virtual void OnClicked() override;
}
Button3, Button4 ...
왜 알아야 하는가?
class Button
{
void OnClicked() { this->_function(); }
void SetFunction(typedef std::function<void()> function)
{
this->_function = function;
}
std::function<void()> _function;
}
왜 알아야 하는가?
Data data = GetSomeData();
Button button;
auto OnClicked = [data]()
{
DoSomethingWithData(data);
...
};
button.SetFunction(OnClicked);
여기서 문제!
Data GetData(int iID)
{
this->_mapData.find(iID)->second;
}
auto& iter = GetData(iID);
Template 타입 추론
template <typename T>
void f(T param);
f(expr);
expr??
T가 레퍼런스나 포인터이고 universal reference가 아닐 때
T가 universal reference 일 때
T가 레퍼런스나 포인터가 아닐 때
Template 타입 추론
레퍼런스나 포인터이고 universal reference가 아닐 때
template <typename T>
void f(T& param);
int x = 22;
const int cx = x;
const int& rx = x;
f(x); // int&
f(cx); // const int&
f(rx); // const int&
Template 타입 추론
레퍼런스나 포인터이고 universal reference가 아닐 때
template <typename T>
void f(const T& param);
int x = 22;
const int cx = x;
const int& rx = x;
f(x); // const int&
f(cx); // const int&
f(rx); // const int&
Template 타입 추론
universal reference 일 때
template <typename T>
void f(T&& param);
int x = 22;
const int cx = x;
const int& rx = x;
f(x); // int&
f(cx); // const int&
f(rx); // const int&
f(22); // int&&
Template 타입 추론
레퍼런스나 포인터가 아닐 때
template <typename T>
void f(T param);
int x = 22;
const int cx = x;
const int& rx = x;
f(x); // int
f(cx); // int
f(rx); // int
auto 타입 추론
레퍼런스나 포인터이고 universal reference가 아닐 때
int x = 22;
const int cx = x;
const int& rx = x;
auto& v1 = x; // int&
auto& v2 = cx; // const int&
auto& v3 = rx; // const int&
const auto& v1 = x; // const int&
const auto& v2 = cx; // const int&
const auto& v3 = rx; // const int&
auto 타입 추론
universal reference 일 때
int x = 22;
const int cx = x;
const int& rx = x;
auto&& v1 = x; // int&
auto&& v2 = cx; // const int&
auto&& v3 = rx; // const int&
auto&& v4 = 27; // int&&
auto 타입 추론
레퍼런스나 포인터가 아닐 때
int x = 22;
const int cx = x;
const int& rx = x;
auto v1 = x; // int
auto v2 = cx; // int
auto v3 = rx; // int
auto 타입 추론
Template 타입 추론과 동일함
하지만 braced initializer!
template <typename T>
void f(T param);
f( { 1, 2, 3 } ); // error!
auto x1 { 1, 2, 3 }; // std::intializer_list<int>
auto x2 = { 1, 2, 3 }; // std::intializer_list<int>
Lambda Capture 타입 추론
By reference : template과 동일
By value : template과 동일하지만 cv 키워드가 유지
const int cx = 0;
auto lam = [cx] {...}
class UpToCompiler
{
private:
const int cx;
...
}
아까 그 문제!
Data GetData(int iID)
{
this->_mapData.find(iID)->second;
}
auto& iter = GetData(iID);

More Related Content

What's hot

[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++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++ 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
 
[C++ Korea] Effective Modern C++ Study item 24-26
[C++ Korea] Effective Modern C++ Study item 24-26[C++ Korea] Effective Modern C++ Study item 24-26
[C++ Korea] Effective Modern C++ Study item 24-26Seok-joon Yun
 
[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
 
[C++ korea] effective modern c++ study item 17 19 신촌 study
[C++ korea] effective modern c++ study item 17 19 신촌 study[C++ korea] effective modern c++ study item 17 19 신촌 study
[C++ korea] effective modern c++ study item 17 19 신촌 studySeok-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
 
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++ 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
 
C++ Advanced 강의 3주차
C++ Advanced 강의 3주차C++ Advanced 강의 3주차
C++ Advanced 강의 3주차HyunJoon Park
 
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기Chris Ohk
 
Visual studio 2010
Visual studio 2010Visual studio 2010
Visual studio 2010MinGeun Park
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉HyunJoon Park
 
[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
 
Pure Function and Rx
Pure Function and RxPure Function and Rx
Pure Function and RxHyungho Ko
 
김재석, C++ 게임 개발자를 위한 c# 활용 기법, 월간 마이크로소프트웨어 창간 28주년 기념 C++ 개발자를 위한 게임 프로그래밍 실전...
김재석, C++ 게임 개발자를 위한 c# 활용 기법, 월간 마이크로소프트웨어 창간 28주년 기념 C++ 개발자를 위한 게임 프로그래밍 실전...김재석, C++ 게임 개발자를 위한 c# 활용 기법, 월간 마이크로소프트웨어 창간 28주년 기념 C++ 개발자를 위한 게임 프로그래밍 실전...
김재석, C++ 게임 개발자를 위한 c# 활용 기법, 월간 마이크로소프트웨어 창간 28주년 기념 C++ 개발자를 위한 게임 프로그래밍 실전...devCAT Studio, NEXON
 
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기Chris Ohk
 
Pure Function and Honest Design
Pure Function and Honest DesignPure Function and Honest Design
Pure Function and Honest DesignHyungho Ko
 
C++ Advanced 강의 5주차
C++ Advanced 강의 5주차C++ Advanced 강의 5주차
C++ Advanced 강의 5주차HyunJoon Park
 

What's hot (20)

[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++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++ 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...
 
[C++ Korea] Effective Modern C++ Study item 24-26
[C++ Korea] Effective Modern C++ Study item 24-26[C++ Korea] Effective Modern C++ Study item 24-26
[C++ Korea] Effective Modern C++ Study item 24-26
 
[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 +윤석준
 
[C++ korea] effective modern c++ study item 17 19 신촌 study
[C++ korea] effective modern c++ study item 17 19 신촌 study[C++ korea] effective modern c++ study item 17 19 신촌 study
[C++ korea] effective modern c++ study item 17 19 신촌 study
 
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 ...
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #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정은식
 
[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...
 
C++ Advanced 강의 3주차
C++ Advanced 강의 3주차C++ Advanced 강의 3주차
C++ Advanced 강의 3주차
 
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
 
Visual studio 2010
Visual studio 2010Visual studio 2010
Visual studio 2010
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉
 
[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
 
Pure Function and Rx
Pure Function and RxPure Function and Rx
Pure Function and Rx
 
김재석, C++ 게임 개발자를 위한 c# 활용 기법, 월간 마이크로소프트웨어 창간 28주년 기념 C++ 개발자를 위한 게임 프로그래밍 실전...
김재석, C++ 게임 개발자를 위한 c# 활용 기법, 월간 마이크로소프트웨어 창간 28주년 기념 C++ 개발자를 위한 게임 프로그래밍 실전...김재석, C++ 게임 개발자를 위한 c# 활용 기법, 월간 마이크로소프트웨어 창간 28주년 기념 C++ 개발자를 위한 게임 프로그래밍 실전...
김재석, C++ 게임 개발자를 위한 c# 활용 기법, 월간 마이크로소프트웨어 창간 28주년 기념 C++ 개발자를 위한 게임 프로그래밍 실전...
 
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
 
Pure Function and Honest Design
Pure Function and Honest DesignPure Function and Honest Design
Pure Function and Honest Design
 
C++ Advanced 강의 5주차
C++ Advanced 강의 5주차C++ Advanced 강의 5주차
C++ Advanced 강의 5주차
 

Viewers also liked

Behavior Tree in Unreal engine 4
Behavior Tree in Unreal engine 4Behavior Tree in Unreal engine 4
Behavior Tree in Unreal engine 4Huey Park
 
Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심흥배 최
 
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
 
언리얼 엔진 4와 함께 프로그래머 없이 게임 만들기
언리얼 엔진 4와 함께 프로그래머 없이 게임 만들기언리얼 엔진 4와 함께 프로그래머 없이 게임 만들기
언리얼 엔진 4와 함께 프로그래머 없이 게임 만들기Huey Park
 
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 (17)

Behavior Tree in Unreal engine 4
Behavior Tree in Unreal engine 4Behavior Tree in Unreal engine 4
Behavior Tree in Unreal engine 4
 
Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심Modern C++ 프로그래머를 위한 CPP11/14 핵심
Modern C++ 프로그래머를 위한 CPP11/14 핵심
 
c++11
c++11c++11
c++11
 
C++11(최지웅)
C++11(최지웅)C++11(최지웅)
C++11(최지웅)
 
C++11
C++11C++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
 
Docker
DockerDocker
Docker
 
초보를 위한 C++11
초보를 위한 C++11초보를 위한 C++11
초보를 위한 C++11
 
Jenkins
JenkinsJenkins
Jenkins
 
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
 
언리얼 엔진 4와 함께 프로그래머 없이 게임 만들기
언리얼 엔진 4와 함께 프로그래머 없이 게임 만들기언리얼 엔진 4와 함께 프로그래머 없이 게임 만들기
언리얼 엔진 4와 함께 프로그래머 없이 게임 만들기
 
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++ 타입 추론

포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++KWANGIL KIM
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기Jongwook Choi
 
Multithread programming 20151206_서진택
Multithread programming 20151206_서진택Multithread programming 20151206_서진택
Multithread programming 20151206_서진택JinTaek Seo
 
불어오는 변화의 바람, 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 명신 김
 
C++’s move semantics
C++’s move semanticsC++’s move semantics
C++’s move semanticsLusain Kim
 
More effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshinMore effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshinDong Chan Shin
 
이펙티브 C++ 스터디
이펙티브 C++ 스터디이펙티브 C++ 스터디
이펙티브 C++ 스터디quxn6
 
[C++ Korea] Effective Modern C++ Study item 31-33
[C++ Korea] Effective Modern C++ Study item 31-33[C++ Korea] Effective Modern C++ Study item 31-33
[C++ Korea] Effective Modern C++ Study item 31-33Seok-joon Yun
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard LibraryDongMin Choi
 
공유 Jdk 7-2-project coin
공유 Jdk 7-2-project coin공유 Jdk 7-2-project coin
공유 Jdk 7-2-project coinknight1128
 
C++20 Key Features Summary
C++20 Key Features SummaryC++20 Key Features Summary
C++20 Key Features SummaryChris Ohk
 
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++Min-soo Park
 
The C++ Programming Language 5장 포인터, 배열, 구조체
The C++ Programming Language 5장 포인터, 배열, 구조체The C++ Programming Language 5장 포인터, 배열, 구조체
The C++ Programming Language 5장 포인터, 배열, 구조체해강
 
Hoons 닷넷 정기세미나
Hoons 닷넷 정기세미나Hoons 닷넷 정기세미나
Hoons 닷넷 정기세미나병걸 윤
 

Similar to C++ 타입 추론 (20)

포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++
 
Changes in c++0x
Changes in c++0xChanges in c++0x
Changes in c++0x
 
Modern effective cpp 항목1
Modern effective cpp 항목1Modern effective cpp 항목1
Modern effective cpp 항목1
 
강의자료3
강의자료3강의자료3
강의자료3
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기
 
Multithread programming 20151206_서진택
Multithread programming 20151206_서진택Multithread programming 20151206_서진택
Multithread programming 20151206_서진택
 
불어오는 변화의 바람, 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
 
C++’s move semantics
C++’s move semanticsC++’s move semantics
C++’s move semantics
 
C review
C  reviewC  review
C review
 
More effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshinMore effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshin
 
이펙티브 C++ 스터디
이펙티브 C++ 스터디이펙티브 C++ 스터디
이펙티브 C++ 스터디
 
[C++ Korea] Effective Modern C++ Study item 31-33
[C++ Korea] Effective Modern C++ Study item 31-33[C++ Korea] Effective Modern C++ Study item 31-33
[C++ Korea] Effective Modern C++ Study item 31-33
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
 
공유 Jdk 7-2-project coin
공유 Jdk 7-2-project coin공유 Jdk 7-2-project coin
공유 Jdk 7-2-project coin
 
C++20 Key Features Summary
C++20 Key Features SummaryC++20 Key Features Summary
C++20 Key Features Summary
 
강의자료 2
강의자료 2강의자료 2
강의자료 2
 
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
 
The C++ Programming Language 5장 포인터, 배열, 구조체
The C++ Programming Language 5장 포인터, 배열, 구조체The C++ Programming Language 5장 포인터, 배열, 구조체
The C++ Programming Language 5장 포인터, 배열, 구조체
 
Hoons 닷넷 정기세미나
Hoons 닷넷 정기세미나Hoons 닷넷 정기세미나
Hoons 닷넷 정기세미나
 

C++ 타입 추론

  • 1. C++ 타입 추론 클라이언트 프로그래머 박재완 template, auto, [](){};
  • 2. 조금만 알아보고 시작합시다 Value C++ 11 이전 lvalue : 표현식 이후에도 지속되는 값 rvalue : 표현식 종료 이후에는 더이상 존재하지 않는 값 int Function(); int x = 3; int y = Function();
  • 3. 조금만 알아보고 시작합시다 Value C++ 11 이후 lvalue : C++ 11 이전의 lvalue prvalue : C++ 11 이전의 rvlaue xvalue : 생명주기가 도래해가는 값, rvlaue reference와 함께 등장, "eXpiring value"의 약자 rvalue : xvalue + prvalue glvalue : lvlaue + xvalue
  • 4. 조금만 알아보고 시작합시다 Universal Reference template<typename T> void f(T&& param) auto&& x; x는 rvalue reference가 아닙니다.
  • 5. 이제부터 타입 추론 C++ 98 template C++ 11 auto, universal reference, lambda capture and return, decltype C++ 14 lambda init captures, function return
  • 6. 왜 알아야 하는가? class Button { virtual void OnClicked(); } class Button2 : public Button { virtual void OnClicked() override; } Button3, Button4 ...
  • 7. 왜 알아야 하는가? class Button { void OnClicked() { this->_function(); } void SetFunction(typedef std::function<void()> function) { this->_function = function; } std::function<void()> _function; }
  • 8. 왜 알아야 하는가? Data data = GetSomeData(); Button button; auto OnClicked = [data]() { DoSomethingWithData(data); ... }; button.SetFunction(OnClicked);
  • 9. 여기서 문제! Data GetData(int iID) { this->_mapData.find(iID)->second; } auto& iter = GetData(iID);
  • 10. Template 타입 추론 template <typename T> void f(T param); f(expr); expr?? T가 레퍼런스나 포인터이고 universal reference가 아닐 때 T가 universal reference 일 때 T가 레퍼런스나 포인터가 아닐 때
  • 11. Template 타입 추론 레퍼런스나 포인터이고 universal reference가 아닐 때 template <typename T> void f(T& param); int x = 22; const int cx = x; const int& rx = x; f(x); // int& f(cx); // const int& f(rx); // const int&
  • 12. Template 타입 추론 레퍼런스나 포인터이고 universal reference가 아닐 때 template <typename T> void f(const T& param); int x = 22; const int cx = x; const int& rx = x; f(x); // const int& f(cx); // const int& f(rx); // const int&
  • 13. Template 타입 추론 universal reference 일 때 template <typename T> void f(T&& param); int x = 22; const int cx = x; const int& rx = x; f(x); // int& f(cx); // const int& f(rx); // const int& f(22); // int&&
  • 14. Template 타입 추론 레퍼런스나 포인터가 아닐 때 template <typename T> void f(T param); int x = 22; const int cx = x; const int& rx = x; f(x); // int f(cx); // int f(rx); // int
  • 15. auto 타입 추론 레퍼런스나 포인터이고 universal reference가 아닐 때 int x = 22; const int cx = x; const int& rx = x; auto& v1 = x; // int& auto& v2 = cx; // const int& auto& v3 = rx; // const int& const auto& v1 = x; // const int& const auto& v2 = cx; // const int& const auto& v3 = rx; // const int&
  • 16. auto 타입 추론 universal reference 일 때 int x = 22; const int cx = x; const int& rx = x; auto&& v1 = x; // int& auto&& v2 = cx; // const int& auto&& v3 = rx; // const int& auto&& v4 = 27; // int&&
  • 17. auto 타입 추론 레퍼런스나 포인터가 아닐 때 int x = 22; const int cx = x; const int& rx = x; auto v1 = x; // int auto v2 = cx; // int auto v3 = rx; // int
  • 18. auto 타입 추론 Template 타입 추론과 동일함 하지만 braced initializer! template <typename T> void f(T param); f( { 1, 2, 3 } ); // error! auto x1 { 1, 2, 3 }; // std::intializer_list<int> auto x2 = { 1, 2, 3 }; // std::intializer_list<int>
  • 19. Lambda Capture 타입 추론 By reference : template과 동일 By value : template과 동일하지만 cv 키워드가 유지 const int cx = 0; auto lam = [cx] {...} class UpToCompiler { private: const int cx; ... }
  • 20. 아까 그 문제! Data GetData(int iID) { this->_mapData.find(iID)->second; } auto& iter = GetData(iID);