SlideShare a Scribd company logo
modern effective c++
1장 형식 연역
- 서론
- 항목1.템플릿 형식 연역 규칙을 숙지하라
서론 – 형식연역(type deduction)
• 타입 추론 아님? (이하 타입 추론)
• C++98에서는 타입 추론에 관한 규칙들이 한 종류
• C++11 에서는 세 가지로 늘어남.
• auto 를 위한 규칙
• decltype 을 위한 규칙
• 기존 규칙 약간 수정(템플릿 등등)
• type을 변경하면 다른 곳에도 영향을 미칠 수 있음.
• 조심해야함…
• 이 장에서 auto와 decltype이 작동하는 방식을 알아보자!
항목 1: 템플릿 타입 추론 규칙을 숙지하라.
• auto 는 템플릿에 대한 타입추론을 기반으로 작동한다.
• 덜 직관적인 경우가 있음.
• 알아보기 어렵다는 뜻…
항목 1: 템플릿 타입 추론 규칙을 숙지하라.
함수 템플릿의 선언의 예:
template<typname T>
void f(ParamType param);
f(expr);
• 여기서 두가지 추론이 사용되는데, 하나는 T에 대한 추론,
• 하나는 ParamType에 대한 추론이다.
항목 1: 템플릿 타입 추론 규칙을 숙지하라.
• ParamType에 const 나 &같은 참조자 수식어가 들어감.
• T 에는 타입이 들어간다.
template<typename T>
void f(const T& param);
int x = 0;
f(x); //T는 int , ParamType은 const &T
// 파라미터 타입은 const int& 로 번역됨.
항목 1: 템플릿 타입 추론 규칙을 숙지하라.
• 이 때, ParamType이 결정되는 과정은 세가지 경우로 나눈다.
1. ParamType 이 포인터나 참조 형식이지만 universal refernce는 아닌
경우.
2. ParamType이 universal reference인 경우.
3. ParamType이 포인터도 아니고 참조도 아닌경우.
1-1 ParamType 이 포인터나 참조 형식이지만 universal
refernce는 아닌경우.
• ParamType 이 포인터나 참조 형식이지만 universal refernce는 아닌경우.
1. 만일 expr부분이 참조 형식이면 참조 부분을 무시한다.
2. 그 다음 expr의 형식을 pattern-matching 방식으로 결정한다.
예>
template <tyepname T>
void f(T& param);
int x = 27;
const int cx = x;
const int& rx = x;
f(x) // int, int&
f(cx) // const int, const int&
f(rx) // const int, const int&
1-1 ParamType 이 포인터나 참조 형식이지만
universal refernce는 아닌경우.
• 둘째, 셋째 호출에서 T가 const int가 되기 때문에 매개변수는
자동으로 const int& 가 된다. 개이득!
• const-ness가 유지된다.
• 세 번째에서 T에 &가 무시된다는것도 명심.
template <tyepname T>
void f(T& param);
int x = 27;
const int cx = x;
const int& rx = x;
f(x) // int, int&
f(cx) // const int, const int&
f(rx) // const int, const int&
1-1 ParamType 이 포인터나 참조 형식이지만
universal refernce는 아닌경우.
• param이 const에 대한 참조로 간주되어 const는 T에서 제외됨.
• rx의 참조성은 무시된다.
template <tyepname T>
void f(const T& param);
int x = 27;
const int cx = x;
const int& rx = x;
f(x) // int, const int&
f(cx) // int, const int&
f(rx) // int, const int&
1-1 ParamType 이 포인터나 참조 형식이지만
universal refernce는 아닌경우.
• 포인터는 const int*로 잘 해석됨.
template <tyepname T>
void f(T* param);
int x = 27;
const int *px = &x;
f(&x); // int, int*
f(px); // const int, const int*
1-2 ParamType이 보편참조일때.
• Universal reference 란?
• lvalue reference와 rvalue reference 둘 다 가능한 상태
template <typname T>
void f(T&& param); // universal ref.
1-2 ParamType이 보편참조일때.
• 이 부분은 항목24에서 다시 설명한대요…
template< typename T>
void f(T&& param);
int x = 27;
const int cx = x;
const int& rx = x;
f(x); //x는 lvalue, int&, int&
f(cx); //cx는 lvalue, const int& , const int&
f(rx); //rx는 lvalue, const int& , const int&
f(27); //27은 rvalue, int, int&&
1-3ParamType이 포인터도 아니고 참조도 아님.
Template<typname T>
void f(T param);
• param은 주어진 인수의 복사본이 된다!
• 이전처럼, expr 부분이 참조이면, 참조는 무시된다.
• 참조성을 무시한후, const이면 const 역시 무시한다.
• volatile 이면 그것도 무시한다. (? 항목 40참조)
1-3ParamType이 포인터도 아니고 참조도 아님.
• const 값을 지정해도, param은 const가 아니다.
template <tyepname T>
void f(T param);
int x = 27;
const int cx = x;
const int& rx = x;
f(x) // int, int
f(cx) // int, int
f(rx) // int, int
• 함수내에 전달되는 값은 무조건 복사되는 값이 된다.
template <tyepname T>
void f(T param);
int x = 27;
const int cx = x;
const int& rx = x;
f(x) // int, int
f(cx) // int, int
f(rx) // int, int
1-3ParamType이 포인터도 아니고 참조도 아님.
• 포인터 자체는 복사되어 const성이 사라짐.
• 가리키는 대상에 대한 const성은 유지됨.
template <tyepname T>
void f(T param);
const char* const ptr =
“Fun with pointers”;
f(ptr); // 포인터값 복사됨
1-3ParamType이 포인터도 아니고 참조도 아님.
1-4 배열인수
• 배일과 포인터를 구분하지 않고 사용할 수 있는경우?
• 많은 경우 배열이 첫 원소를 가리키는 포인터로 decay된다.
• 그렇다면 템플릿에서는 어떨까?
1-4 배열인수
• void f(T& param); 으로 선언했다면 실제로 전달!
• const char (&)[13] 으로 추론된다.
template <tyepname T>
void f(T param);
const char name[] = “J. P. Briggs”;
// const char[13]
const char * ptrToName = name;
// decay
f(name); //const char*
// 비교
void myFunc (int param[]);
//포인터로 decay
1-4 배열인수
• 개멋있다…
int keyVals[] = { 1,2,3,4,5}
int sameSizeVals[ arraySize(keyVals) ]; //이런식으로 같은크기 선언도 가능
std::array<int, arraySize(keyVals)> sameSizeVals; //C++ style
noexcept 는 항목 14참고.
// 배열의 크기를 컴파일 시점에서 상수로 리턴하는 템플릿…
template<typename T, size_t N>
constexpr size_t arrSize(T(&)[N]) noexcept
{
return N;
}
1-5 함수 인수
• 함수형식도 함수 포인터로 decay 된다.
void somFunc(int, double);
template<typename T>
void f1(T param);
template<typename T>
void f2(T& param);
f1(somFunc); // param은 함수 포인터로 추론됨.
// void (*)( int, double )
f2(somFunc); // param은 함수 참조로 연역됨.
// void (&)( int, double )
항목 1: 템플릿 타입 추론 규칙을 숙지하라.
• 정리
• 은근 간단한데
• 왼값 관련 특별규정에 유의…
• 포인터 decay 관련 해서 유의…
• 대체 어떻게 추론 된건지 템플릿 결과물을 알고 싶으면 항목 4
참조.
끝
• 감사합니다.
• 출처
• 책 : modern effective c++

More Related Content

What's hot

[2011 05 21] 4장 제어
[2011 05 21] 4장 제어[2011 05 21] 4장 제어
[2011 05 21] 4장 제어Jong Pil Won
 
7 mid term summary
7 mid term summary7 mid term summary
7 mid term summary웅식 전
 
파이썬 숫자,변수,문자열
파이썬 숫자,변수,문자열파이썬 숫자,변수,문자열
파이썬 숫자,변수,문자열HoYong Na
 
데이터베이스 정규화
데이터베이스 정규화데이터베이스 정규화
데이터베이스 정규화Hoyoung Jung
 
HI-ARC 정기모임 #7 BFS
HI-ARC 정기모임 #7 BFSHI-ARC 정기모임 #7 BFS
HI-ARC 정기모임 #7 BFSJae-yeol Lee
 
python 수학이해하기
python 수학이해하기python 수학이해하기
python 수학이해하기Yong Joon Moon
 
More effective c++ Chap1~2
More effective c++ Chap1~2More effective c++ Chap1~2
More effective c++ Chap1~2Injae Lee
 
[0521 석재호]백트래킹알고리즘
[0521 석재호]백트래킹알고리즘[0521 석재호]백트래킹알고리즘
[0521 석재호]백트래킹알고리즘Jaeho Seok
 
Start IoT with JavaScript - 4.객체1
Start IoT with JavaScript - 4.객체1Start IoT with JavaScript - 4.객체1
Start IoT with JavaScript - 4.객체1Park Jonggun
 
Start IoT with JavaScript - 2.연산자
Start IoT with JavaScript - 2.연산자Start IoT with JavaScript - 2.연산자
Start IoT with JavaScript - 2.연산자Park Jonggun
 
2532조기완 값형식
2532조기완 값형식2532조기완 값형식
2532조기완 값형식기완 조
 
이펙티브 C++ 5,6 장 스터디
이펙티브 C++ 5,6 장 스터디이펙티브 C++ 5,6 장 스터디
이펙티브 C++ 5,6 장 스터디quxn6
 
자바 스터디(6기) 1
자바 스터디(6기) 1자바 스터디(6기) 1
자바 스터디(6기) 1Jina Lee
 
포인터의 기초(1)
포인터의 기초(1)포인터의 기초(1)
포인터의 기초(1)Hoyoung Jung
 
포인터의기초 (2) - 포인터 사용하기1
포인터의기초 (2) - 포인터 사용하기1포인터의기초 (2) - 포인터 사용하기1
포인터의기초 (2) - 포인터 사용하기1Hoyoung Jung
 
Start IoT with JavaScript - 5.객체2
Start IoT with JavaScript - 5.객체2Start IoT with JavaScript - 5.객체2
Start IoT with JavaScript - 5.객체2Park Jonggun
 

What's hot (20)

[2011 05 21] 4장 제어
[2011 05 21] 4장 제어[2011 05 21] 4장 제어
[2011 05 21] 4장 제어
 
7 mid term summary
7 mid term summary7 mid term summary
7 mid term summary
 
파이썬 숫자,변수,문자열
파이썬 숫자,변수,문자열파이썬 숫자,변수,문자열
파이썬 숫자,변수,문자열
 
데이터베이스 정규화
데이터베이스 정규화데이터베이스 정규화
데이터베이스 정규화
 
Java generics
Java genericsJava generics
Java generics
 
HI-ARC 정기모임 #7 BFS
HI-ARC 정기모임 #7 BFSHI-ARC 정기모임 #7 BFS
HI-ARC 정기모임 #7 BFS
 
python 수학이해하기
python 수학이해하기python 수학이해하기
python 수학이해하기
 
More effective c++ Chap1~2
More effective c++ Chap1~2More effective c++ Chap1~2
More effective c++ Chap1~2
 
[0521 석재호]백트래킹알고리즘
[0521 석재호]백트래킹알고리즘[0521 석재호]백트래킹알고리즘
[0521 석재호]백트래킹알고리즘
 
Start IoT with JavaScript - 4.객체1
Start IoT with JavaScript - 4.객체1Start IoT with JavaScript - 4.객체1
Start IoT with JavaScript - 4.객체1
 
Start IoT with JavaScript - 2.연산자
Start IoT with JavaScript - 2.연산자Start IoT with JavaScript - 2.연산자
Start IoT with JavaScript - 2.연산자
 
Java standard(8~13)
Java standard(8~13)Java standard(8~13)
Java standard(8~13)
 
2532조기완 값형식
2532조기완 값형식2532조기완 값형식
2532조기완 값형식
 
이펙티브 C++ 5,6 장 스터디
이펙티브 C++ 5,6 장 스터디이펙티브 C++ 5,6 장 스터디
이펙티브 C++ 5,6 장 스터디
 
자바 스터디(6기) 1
자바 스터디(6기) 1자바 스터디(6기) 1
자바 스터디(6기) 1
 
포인터의 기초(1)
포인터의 기초(1)포인터의 기초(1)
포인터의 기초(1)
 
3.포인터
3.포인터3.포인터
3.포인터
 
포인터의기초 (2) - 포인터 사용하기1
포인터의기초 (2) - 포인터 사용하기1포인터의기초 (2) - 포인터 사용하기1
포인터의기초 (2) - 포인터 사용하기1
 
Start IoT with JavaScript - 5.객체2
Start IoT with JavaScript - 5.객체2Start IoT with JavaScript - 5.객체2
Start IoT with JavaScript - 5.객체2
 
Equation Solving
Equation SolvingEquation Solving
Equation Solving
 

Viewers also liked

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
 
Modern effective c++ 항목 3
Modern effective c++ 항목 3Modern effective c++ 항목 3
Modern effective c++ 항목 3ssuser7c5a40
 
TCP가 실패하는 상황들
TCP가 실패하는 상황들TCP가 실패하는 상황들
TCP가 실패하는 상황들ssuser7c5a40
 
Effective c++ 정리 chapter 4
Effective c++ 정리 chapter 4Effective c++ 정리 chapter 4
Effective c++ 정리 chapter 4연우 김
 
Effective c++ 2
Effective c++ 2Effective c++ 2
Effective c++ 2현찬 양
 
C++정리 스마트포인터
C++정리 스마트포인터C++정리 스마트포인터
C++정리 스마트포인터fefe7270
 
Stagefright recorder part1
Stagefright recorder part1Stagefright recorder part1
Stagefright recorder part1fefe7270
 
Effective C++ 정리 chapter 3
Effective C++ 정리 chapter 3Effective C++ 정리 chapter 3
Effective C++ 정리 chapter 3연우 김
 
실전프로젝트 정서경 양현찬
실전프로젝트 정서경 양현찬실전프로젝트 정서경 양현찬
실전프로젝트 정서경 양현찬현찬 양
 
GPG 1.2 템플릿 메타프로그래밍을 이용한 빠른 수학 연산
GPG 1.2 템플릿 메타프로그래밍을 이용한 빠른 수학 연산GPG 1.2 템플릿 메타프로그래밍을 이용한 빠른 수학 연산
GPG 1.2 템플릿 메타프로그래밍을 이용한 빠른 수학 연산Taeung Ra
 
Effective c++chapter1 and2
Effective c++chapter1 and2Effective c++chapter1 and2
Effective c++chapter1 and2성연 김
 

Viewers also liked (12)

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 ...
 
Modern effective c++ 항목 3
Modern effective c++ 항목 3Modern effective c++ 항목 3
Modern effective c++ 항목 3
 
TCP가 실패하는 상황들
TCP가 실패하는 상황들TCP가 실패하는 상황들
TCP가 실패하는 상황들
 
Effective c++ 정리 chapter 4
Effective c++ 정리 chapter 4Effective c++ 정리 chapter 4
Effective c++ 정리 chapter 4
 
Effective c++ 2
Effective c++ 2Effective c++ 2
Effective c++ 2
 
C++정리 스마트포인터
C++정리 스마트포인터C++정리 스마트포인터
C++정리 스마트포인터
 
Stagefright recorder part1
Stagefright recorder part1Stagefright recorder part1
Stagefright recorder part1
 
Effective C++ 정리 chapter 3
Effective C++ 정리 chapter 3Effective C++ 정리 chapter 3
Effective C++ 정리 chapter 3
 
Titanic with r
Titanic with rTitanic with r
Titanic with r
 
실전프로젝트 정서경 양현찬
실전프로젝트 정서경 양현찬실전프로젝트 정서경 양현찬
실전프로젝트 정서경 양현찬
 
GPG 1.2 템플릿 메타프로그래밍을 이용한 빠른 수학 연산
GPG 1.2 템플릿 메타프로그래밍을 이용한 빠른 수학 연산GPG 1.2 템플릿 메타프로그래밍을 이용한 빠른 수학 연산
GPG 1.2 템플릿 메타프로그래밍을 이용한 빠른 수학 연산
 
Effective c++chapter1 and2
Effective c++chapter1 and2Effective c++chapter1 and2
Effective c++chapter1 and2
 

Similar to Modern effective cpp 항목1

C++ 타입 추론
C++ 타입 추론C++ 타입 추론
C++ 타입 추론Huey Park
 
연산자 오버로딩
연산자 오버로딩연산자 오버로딩
연산자 오버로딩수빈 박
 
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건 손
 
[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
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1Chris Ohk
 
Ec++ 3,4 summary
Ec++ 3,4 summaryEc++ 3,4 summary
Ec++ 3,4 summarySehyeon Nam
 
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
 
6장 표현식 및 문장
6장 표현식 및 문장6장 표현식 및 문장
6장 표현식 및 문장재정 이
 
C++ Advanced 강의 5주차
C++ Advanced 강의 5주차C++ Advanced 강의 5주차
C++ Advanced 강의 5주차HyunJoon Park
 
Python3 brief summary
Python3 brief summaryPython3 brief summary
Python3 brief summaryHoChul Shin
 
7.woring with text data
7.woring with text data7.woring with text data
7.woring with text dataHaesun Park
 
[Swift] Protocol (1/2)
[Swift] Protocol (1/2)[Swift] Protocol (1/2)
[Swift] Protocol (1/2)Bill Kim
 
Abstract syntax semantic analyze
Abstract syntax semantic analyzeAbstract syntax semantic analyze
Abstract syntax semantic analyzeHyunJoon Park
 
파이썬 스터디 9장
파이썬 스터디 9장파이썬 스터디 9장
파이썬 스터디 9장SeongHyun Ahn
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉HyunJoon Park
 
C++’s move semantics
C++’s move semanticsC++’s move semantics
C++’s move semanticsLusain Kim
 
C++ Advanced 강의 3주차
C++ Advanced 강의 3주차C++ Advanced 강의 3주차
C++ Advanced 강의 3주차HyunJoon Park
 
Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약Nam Hyeonuk
 
Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2Chris Ohk
 

Similar to Modern effective cpp 항목1 (20)

C++ 타입 추론
C++ 타입 추론C++ 타입 추론
C++ 타입 추론
 
연산자 오버로딩
연산자 오버로딩연산자 오버로딩
연산자 오버로딩
 
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
 
[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
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1
 
Ec++ 3,4 summary
Ec++ 3,4 summaryEc++ 3,4 summary
Ec++ 3,4 summary
 
Python
PythonPython
Python
 
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++
 
6장 표현식 및 문장
6장 표현식 및 문장6장 표현식 및 문장
6장 표현식 및 문장
 
C++ Advanced 강의 5주차
C++ Advanced 강의 5주차C++ Advanced 강의 5주차
C++ Advanced 강의 5주차
 
Python3 brief summary
Python3 brief summaryPython3 brief summary
Python3 brief summary
 
7.woring with text data
7.woring with text data7.woring with text data
7.woring with text data
 
[Swift] Protocol (1/2)
[Swift] Protocol (1/2)[Swift] Protocol (1/2)
[Swift] Protocol (1/2)
 
Abstract syntax semantic analyze
Abstract syntax semantic analyzeAbstract syntax semantic analyze
Abstract syntax semantic analyze
 
파이썬 스터디 9장
파이썬 스터디 9장파이썬 스터디 9장
파이썬 스터디 9장
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉
 
C++’s move semantics
C++’s move semanticsC++’s move semantics
C++’s move semantics
 
C++ Advanced 강의 3주차
C++ Advanced 강의 3주차C++ Advanced 강의 3주차
C++ Advanced 강의 3주차
 
Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약
 
Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2
 

Modern effective cpp 항목1

  • 1. modern effective c++ 1장 형식 연역 - 서론 - 항목1.템플릿 형식 연역 규칙을 숙지하라
  • 2. 서론 – 형식연역(type deduction) • 타입 추론 아님? (이하 타입 추론) • C++98에서는 타입 추론에 관한 규칙들이 한 종류 • C++11 에서는 세 가지로 늘어남. • auto 를 위한 규칙 • decltype 을 위한 규칙 • 기존 규칙 약간 수정(템플릿 등등) • type을 변경하면 다른 곳에도 영향을 미칠 수 있음. • 조심해야함… • 이 장에서 auto와 decltype이 작동하는 방식을 알아보자!
  • 3. 항목 1: 템플릿 타입 추론 규칙을 숙지하라. • auto 는 템플릿에 대한 타입추론을 기반으로 작동한다. • 덜 직관적인 경우가 있음. • 알아보기 어렵다는 뜻…
  • 4. 항목 1: 템플릿 타입 추론 규칙을 숙지하라. 함수 템플릿의 선언의 예: template<typname T> void f(ParamType param); f(expr); • 여기서 두가지 추론이 사용되는데, 하나는 T에 대한 추론, • 하나는 ParamType에 대한 추론이다.
  • 5. 항목 1: 템플릿 타입 추론 규칙을 숙지하라. • ParamType에 const 나 &같은 참조자 수식어가 들어감. • T 에는 타입이 들어간다. template<typename T> void f(const T& param); int x = 0; f(x); //T는 int , ParamType은 const &T // 파라미터 타입은 const int& 로 번역됨.
  • 6. 항목 1: 템플릿 타입 추론 규칙을 숙지하라. • 이 때, ParamType이 결정되는 과정은 세가지 경우로 나눈다. 1. ParamType 이 포인터나 참조 형식이지만 universal refernce는 아닌 경우. 2. ParamType이 universal reference인 경우. 3. ParamType이 포인터도 아니고 참조도 아닌경우.
  • 7. 1-1 ParamType 이 포인터나 참조 형식이지만 universal refernce는 아닌경우. • ParamType 이 포인터나 참조 형식이지만 universal refernce는 아닌경우. 1. 만일 expr부분이 참조 형식이면 참조 부분을 무시한다. 2. 그 다음 expr의 형식을 pattern-matching 방식으로 결정한다. 예> template <tyepname T> void f(T& param); int x = 27; const int cx = x; const int& rx = x; f(x) // int, int& f(cx) // const int, const int& f(rx) // const int, const int&
  • 8. 1-1 ParamType 이 포인터나 참조 형식이지만 universal refernce는 아닌경우. • 둘째, 셋째 호출에서 T가 const int가 되기 때문에 매개변수는 자동으로 const int& 가 된다. 개이득! • const-ness가 유지된다. • 세 번째에서 T에 &가 무시된다는것도 명심. template <tyepname T> void f(T& param); int x = 27; const int cx = x; const int& rx = x; f(x) // int, int& f(cx) // const int, const int& f(rx) // const int, const int&
  • 9. 1-1 ParamType 이 포인터나 참조 형식이지만 universal refernce는 아닌경우. • param이 const에 대한 참조로 간주되어 const는 T에서 제외됨. • rx의 참조성은 무시된다. template <tyepname T> void f(const T& param); int x = 27; const int cx = x; const int& rx = x; f(x) // int, const int& f(cx) // int, const int& f(rx) // int, const int&
  • 10. 1-1 ParamType 이 포인터나 참조 형식이지만 universal refernce는 아닌경우. • 포인터는 const int*로 잘 해석됨. template <tyepname T> void f(T* param); int x = 27; const int *px = &x; f(&x); // int, int* f(px); // const int, const int*
  • 11. 1-2 ParamType이 보편참조일때. • Universal reference 란? • lvalue reference와 rvalue reference 둘 다 가능한 상태 template <typname T> void f(T&& param); // universal ref.
  • 12. 1-2 ParamType이 보편참조일때. • 이 부분은 항목24에서 다시 설명한대요… template< typename T> void f(T&& param); int x = 27; const int cx = x; const int& rx = x; f(x); //x는 lvalue, int&, int& f(cx); //cx는 lvalue, const int& , const int& f(rx); //rx는 lvalue, const int& , const int& f(27); //27은 rvalue, int, int&&
  • 13. 1-3ParamType이 포인터도 아니고 참조도 아님. Template<typname T> void f(T param); • param은 주어진 인수의 복사본이 된다! • 이전처럼, expr 부분이 참조이면, 참조는 무시된다. • 참조성을 무시한후, const이면 const 역시 무시한다. • volatile 이면 그것도 무시한다. (? 항목 40참조)
  • 14. 1-3ParamType이 포인터도 아니고 참조도 아님. • const 값을 지정해도, param은 const가 아니다. template <tyepname T> void f(T param); int x = 27; const int cx = x; const int& rx = x; f(x) // int, int f(cx) // int, int f(rx) // int, int
  • 15. • 함수내에 전달되는 값은 무조건 복사되는 값이 된다. template <tyepname T> void f(T param); int x = 27; const int cx = x; const int& rx = x; f(x) // int, int f(cx) // int, int f(rx) // int, int 1-3ParamType이 포인터도 아니고 참조도 아님.
  • 16. • 포인터 자체는 복사되어 const성이 사라짐. • 가리키는 대상에 대한 const성은 유지됨. template <tyepname T> void f(T param); const char* const ptr = “Fun with pointers”; f(ptr); // 포인터값 복사됨 1-3ParamType이 포인터도 아니고 참조도 아님.
  • 17. 1-4 배열인수 • 배일과 포인터를 구분하지 않고 사용할 수 있는경우? • 많은 경우 배열이 첫 원소를 가리키는 포인터로 decay된다. • 그렇다면 템플릿에서는 어떨까?
  • 18. 1-4 배열인수 • void f(T& param); 으로 선언했다면 실제로 전달! • const char (&)[13] 으로 추론된다. template <tyepname T> void f(T param); const char name[] = “J. P. Briggs”; // const char[13] const char * ptrToName = name; // decay f(name); //const char* // 비교 void myFunc (int param[]); //포인터로 decay
  • 19. 1-4 배열인수 • 개멋있다… int keyVals[] = { 1,2,3,4,5} int sameSizeVals[ arraySize(keyVals) ]; //이런식으로 같은크기 선언도 가능 std::array<int, arraySize(keyVals)> sameSizeVals; //C++ style noexcept 는 항목 14참고. // 배열의 크기를 컴파일 시점에서 상수로 리턴하는 템플릿… template<typename T, size_t N> constexpr size_t arrSize(T(&)[N]) noexcept { return N; }
  • 20. 1-5 함수 인수 • 함수형식도 함수 포인터로 decay 된다. void somFunc(int, double); template<typename T> void f1(T param); template<typename T> void f2(T& param); f1(somFunc); // param은 함수 포인터로 추론됨. // void (*)( int, double ) f2(somFunc); // param은 함수 참조로 연역됨. // void (&)( int, double )
  • 21. 항목 1: 템플릿 타입 추론 규칙을 숙지하라. • 정리 • 은근 간단한데 • 왼값 관련 특별규정에 유의… • 포인터 decay 관련 해서 유의… • 대체 어떻게 추론 된건지 템플릿 결과물을 알고 싶으면 항목 4 참조.
  • 22. 끝 • 감사합니다. • 출처 • 책 : modern effective c++