SlideShare a Scribd company logo
1 of 13
Download to read offline
Effective C++
item45. “호환되는 모든 타입”을 받아들이는 데는
멤버 함수 템플릿이 직방!
이데아게임즈 손진화
템플릿으로 암시적 변환 구현하기
- 스마트 포인터를
구현하고자 한다
- 모든 타입에
대응하게 하기
위해 템플릿 사용
템플릿으로 암시적 변환 구현하기
템플릿은 암시적 변환을 지원하지 않는다
직접 복사 생성자를 정의
- 템플릿 특수화를 한 후 원하는 타입의 복사 생성자를 직접 선언
- 원하는 모든 타입에 대한 복사 생성자를 작성할 수는 없다
멤버 함수 템플릿으로 복사 생성자를 작성
- 멤버 함수 템플릿을 이용하면 어떤 클래스의 멤버 함수를 다양한
타입으로 찍어낼 수 있다
- 이렇게 만든 생성자를 일반화 복사 생성자 (generalized copy
constructor) 라고 한다
- Raw 포인터처럼 암시적 변환을 지원할 수 있도록 explicit 키워드
제거
멤버 함수 템플릿으로 복사 생성자를 작성
- 모든 타입에 대해 복사 생성자가 만들어 질 수 있기 때문에 문제가
발생할 여지가 있다
멤버 함수 템플릿으로 복사 생성자를 작성
- 타입 U*에서 타입 T*로 암시적 변환이 가능할 때만 컴파일이
되도록 작동한다
shared_ptr 의 멤버 함수 템플릿
호환되는 모든 기본 제공 포인터 생성자
shared_ptr, weak_ptr 에 대응하는 생성자
shared_ptr 을 통한 암시적 변환만 허용하고 있다
shared_ptr 의 멤버 함수 템플릿
- template <bool B, class T = void> struct enable_if
- template <class From, class To> struct is_convertible
- unique_ptr 대입 생성자
- unique_ptr 은 shared_ptr에 이동되기 때문에 const 키워드가
빠져있고 오른값으로 들어온다
shared_ptr 의 멤버 함수 템플릿
shared_ptr, unique_ptr 대입 연산자
기본 복사 생성자, 일반화 복사 생성자
일반화 복사 생성자가 있어도 호출 되지 않는다
기본 복사 생성자, 일반화 복사 생성자
일반화 복사 생성자가 있어도 기본 생성자는 생성됨을 알 수 있다
이것만은 잊지 말자!
- 호환되는 모든 타입을 받아들이는 멤버 함수를 만들려면 멤버
함수 템플릿을 사용합시다
- 일반화된 복사 생성 연산과 일반화된 대입 연산을 위해 멤버
템플릿을 선언했다 하더라도, 보통의 복사 생성자와 복사 대입
연산자는 여전히 직접 선언해야 합니다

More Related Content

What's hot

Droid Knight 2019
Droid Knight 2019Droid Knight 2019
Droid Knight 2019선옥 장
 
Droid knights 2019 - (Large-scale App을 위한) Android Architecture 총정리
Droid knights 2019 - (Large-scale App을 위한) Android Architecture 총정리Droid knights 2019 - (Large-scale App을 위한) Android Architecture 총정리
Droid knights 2019 - (Large-scale App을 위한) Android Architecture 총정리Sa-ryong Kang
 
형상관리 발표자료 안정민
형상관리 발표자료 안정민형상관리 발표자료 안정민
형상관리 발표자료 안정민정민 안
 
iOS Modular Architecture with Tuist
iOS Modular Architecture with TuistiOS Modular Architecture with Tuist
iOS Modular Architecture with Tuist정민 안
 
Angular CodeLab 두번째
Angular CodeLab 두번째Angular CodeLab 두번째
Angular CodeLab 두번째SangHun Lee
 
Kotlin 을 아시나요?
Kotlin 을 아시나요?Kotlin 을 아시나요?
Kotlin 을 아시나요?NBT Inc.
 
200819 NAVER TECH CONCERT 01_100만 달러짜리 빠른 앱을 만드는 비법 전수
200819 NAVER TECH CONCERT 01_100만 달러짜리 빠른 앱을 만드는 비법 전수200819 NAVER TECH CONCERT 01_100만 달러짜리 빠른 앱을 만드는 비법 전수
200819 NAVER TECH CONCERT 01_100만 달러짜리 빠른 앱을 만드는 비법 전수NAVER Engineering
 
모두의 클라우드 딥러닝
모두의 클라우드 딥러닝모두의 클라우드 딥러닝
모두의 클라우드 딥러닝NAVER Engineering
 
ant로 안드로이드 앱을 자동으로 빌드하자
ant로 안드로이드 앱을 자동으로 빌드하자ant로 안드로이드 앱을 자동으로 빌드하자
ant로 안드로이드 앱을 자동으로 빌드하자Sewon Ann
 
[TECHCON 2019: MOBILE - Android]3.안드로이드 개발자 로드맵
[TECHCON 2019: MOBILE - Android]3.안드로이드 개발자 로드맵[TECHCON 2019: MOBILE - Android]3.안드로이드 개발자 로드맵
[TECHCON 2019: MOBILE - Android]3.안드로이드 개발자 로드맵NAVER Engineering
 

What's hot (11)

React vac pattern
React vac patternReact vac pattern
React vac pattern
 
Droid Knight 2019
Droid Knight 2019Droid Knight 2019
Droid Knight 2019
 
Droid knights 2019 - (Large-scale App을 위한) Android Architecture 총정리
Droid knights 2019 - (Large-scale App을 위한) Android Architecture 총정리Droid knights 2019 - (Large-scale App을 위한) Android Architecture 총정리
Droid knights 2019 - (Large-scale App을 위한) Android Architecture 총정리
 
형상관리 발표자료 안정민
형상관리 발표자료 안정민형상관리 발표자료 안정민
형상관리 발표자료 안정민
 
iOS Modular Architecture with Tuist
iOS Modular Architecture with TuistiOS Modular Architecture with Tuist
iOS Modular Architecture with Tuist
 
Angular CodeLab 두번째
Angular CodeLab 두번째Angular CodeLab 두번째
Angular CodeLab 두번째
 
Kotlin 을 아시나요?
Kotlin 을 아시나요?Kotlin 을 아시나요?
Kotlin 을 아시나요?
 
200819 NAVER TECH CONCERT 01_100만 달러짜리 빠른 앱을 만드는 비법 전수
200819 NAVER TECH CONCERT 01_100만 달러짜리 빠른 앱을 만드는 비법 전수200819 NAVER TECH CONCERT 01_100만 달러짜리 빠른 앱을 만드는 비법 전수
200819 NAVER TECH CONCERT 01_100만 달러짜리 빠른 앱을 만드는 비법 전수
 
모두의 클라우드 딥러닝
모두의 클라우드 딥러닝모두의 클라우드 딥러닝
모두의 클라우드 딥러닝
 
ant로 안드로이드 앱을 자동으로 빌드하자
ant로 안드로이드 앱을 자동으로 빌드하자ant로 안드로이드 앱을 자동으로 빌드하자
ant로 안드로이드 앱을 자동으로 빌드하자
 
[TECHCON 2019: MOBILE - Android]3.안드로이드 개발자 로드맵
[TECHCON 2019: MOBILE - Android]3.안드로이드 개발자 로드맵[TECHCON 2019: MOBILE - Android]3.안드로이드 개발자 로드맵
[TECHCON 2019: MOBILE - Android]3.안드로이드 개발자 로드맵
 

Similar to Effective c++ item45

Effective c++ 4
Effective c++ 4Effective c++ 4
Effective c++ 4현찬 양
 
OpenJigWare(V02.00.04)
OpenJigWare(V02.00.04)OpenJigWare(V02.00.04)
OpenJigWare(V02.00.04)Jinwook On
 
디자인 패턴 적용
디자인 패턴 적용디자인 패턴 적용
디자인 패턴 적용Sean Choi
 
M1 2 1
M1 2 1M1 2 1
M1 2 1nexthw
 
Stonze study week1
Stonze study week1Stonze study week1
Stonze study week1Injae Lee
 
타입스크립트 잘 사용하기
타입스크립트 잘 사용하기타입스크립트 잘 사용하기
타입스크립트 잘 사용하기SanghoYun
 
Effective c++ 1~8장
Effective c++ 1~8장 Effective c++ 1~8장
Effective c++ 1~8장 Shin heemin
 
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍Chris Ohk
 
프로젝트 관리 및 지켜야 할 사항들
프로젝트 관리 및 지켜야 할 사항들프로젝트 관리 및 지켜야 할 사항들
프로젝트 관리 및 지켜야 할 사항들Lee Geonhee
 
Dagger with multi modules
Dagger with multi modulesDagger with multi modules
Dagger with multi modulesYoung-Hyuk Yoo
 
[0820 석재호]headfirst디자인패턴
[0820 석재호]headfirst디자인패턴[0820 석재호]headfirst디자인패턴
[0820 석재호]headfirst디자인패턴Jaeho Seok
 
NDC 2015 삼시세끼 빌드만들기
NDC 2015 삼시세끼 빌드만들기NDC 2015 삼시세끼 빌드만들기
NDC 2015 삼시세끼 빌드만들기Hyunsuk Ahn
 
Effective c++ chapter 7,8
Effective c++ chapter 7,8Effective c++ chapter 7,8
Effective c++ chapter 7,8문익 장
 
Effective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshinEffective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshinDong Chan Shin
 

Similar to Effective c++ item45 (17)

5 6 1
5 6 15 6 1
5 6 1
 
7 8 1
7 8 17 8 1
7 8 1
 
Effective c++ 4
Effective c++ 4Effective c++ 4
Effective c++ 4
 
OpenJigWare(V02.00.04)
OpenJigWare(V02.00.04)OpenJigWare(V02.00.04)
OpenJigWare(V02.00.04)
 
디자인 패턴 적용
디자인 패턴 적용디자인 패턴 적용
디자인 패턴 적용
 
M1 2 1
M1 2 1M1 2 1
M1 2 1
 
Stonze study week1
Stonze study week1Stonze study week1
Stonze study week1
 
타입스크립트 잘 사용하기
타입스크립트 잘 사용하기타입스크립트 잘 사용하기
타입스크립트 잘 사용하기
 
Effective c++ 1~8장
Effective c++ 1~8장 Effective c++ 1~8장
Effective c++ 1~8장
 
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
 
프로젝트 관리 및 지켜야 할 사항들
프로젝트 관리 및 지켜야 할 사항들프로젝트 관리 및 지켜야 할 사항들
프로젝트 관리 및 지켜야 할 사항들
 
Dagger with multi modules
Dagger with multi modulesDagger with multi modules
Dagger with multi modules
 
1 2 1
1 2 11 2 1
1 2 1
 
[0820 석재호]headfirst디자인패턴
[0820 석재호]headfirst디자인패턴[0820 석재호]headfirst디자인패턴
[0820 석재호]headfirst디자인패턴
 
NDC 2015 삼시세끼 빌드만들기
NDC 2015 삼시세끼 빌드만들기NDC 2015 삼시세끼 빌드만들기
NDC 2015 삼시세끼 빌드만들기
 
Effective c++ chapter 7,8
Effective c++ chapter 7,8Effective c++ chapter 7,8
Effective c++ chapter 7,8
 
Effective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshinEffective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshin
 

More from 진화 손

C++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdfC++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdf진화 손
 
C++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdfC++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdf진화 손
 
C++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templatesC++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templates진화 손
 
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...진화 손
 
C++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functionsC++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functions진화 손
 
C++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdfC++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdf진화 손
 
C++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete typesC++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete types진화 손
 
C++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirementsC++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirements진화 손
 
C++20 Concepts library
C++20 Concepts libraryC++20 Concepts library
C++20 Concepts library진화 손
 
C++20 Coroutine
C++20 CoroutineC++20 Coroutine
C++20 Coroutine진화 손
 
C++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rulesC++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rules진화 손
 
C++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rulesC++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rules진화 손
 
C++20 explicit(bool)
C++20 explicit(bool)C++20 explicit(bool)
C++20 explicit(bool)진화 손
 
C++20 std::map::contains
C++20 std::map::containsC++20 std::map::contains
C++20 std::map::contains진화 손
 
C++20 Comparing unordered containers
C++20 Comparing unordered containersC++20 Comparing unordered containers
C++20 Comparing unordered containers진화 손
 
C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]진화 손
 
C++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contextsC++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contexts진화 손
 
C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>진화 손
 
C++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptrC++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptr진화 손
 
C++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fieldsC++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fields진화 손
 

More from 진화 손 (20)

C++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdfC++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdf
 
C++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdfC++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdf
 
C++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templatesC++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templates
 
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
 
C++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functionsC++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functions
 
C++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdfC++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdf
 
C++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete typesC++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete types
 
C++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirementsC++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirements
 
C++20 Concepts library
C++20 Concepts libraryC++20 Concepts library
C++20 Concepts library
 
C++20 Coroutine
C++20 CoroutineC++20 Coroutine
C++20 Coroutine
 
C++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rulesC++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rules
 
C++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rulesC++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rules
 
C++20 explicit(bool)
C++20 explicit(bool)C++20 explicit(bool)
C++20 explicit(bool)
 
C++20 std::map::contains
C++20 std::map::containsC++20 std::map::contains
C++20 std::map::contains
 
C++20 Comparing unordered containers
C++20 Comparing unordered containersC++20 Comparing unordered containers
C++20 Comparing unordered containers
 
C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]
 
C++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contextsC++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contexts
 
C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>
 
C++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptrC++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptr
 
C++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fieldsC++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fields
 

Effective c++ item45

  • 1. Effective C++ item45. “호환되는 모든 타입”을 받아들이는 데는 멤버 함수 템플릿이 직방! 이데아게임즈 손진화
  • 2. 템플릿으로 암시적 변환 구현하기 - 스마트 포인터를 구현하고자 한다 - 모든 타입에 대응하게 하기 위해 템플릿 사용
  • 3. 템플릿으로 암시적 변환 구현하기 템플릿은 암시적 변환을 지원하지 않는다
  • 4. 직접 복사 생성자를 정의 - 템플릿 특수화를 한 후 원하는 타입의 복사 생성자를 직접 선언 - 원하는 모든 타입에 대한 복사 생성자를 작성할 수는 없다
  • 5. 멤버 함수 템플릿으로 복사 생성자를 작성 - 멤버 함수 템플릿을 이용하면 어떤 클래스의 멤버 함수를 다양한 타입으로 찍어낼 수 있다 - 이렇게 만든 생성자를 일반화 복사 생성자 (generalized copy constructor) 라고 한다 - Raw 포인터처럼 암시적 변환을 지원할 수 있도록 explicit 키워드 제거
  • 6. 멤버 함수 템플릿으로 복사 생성자를 작성 - 모든 타입에 대해 복사 생성자가 만들어 질 수 있기 때문에 문제가 발생할 여지가 있다
  • 7. 멤버 함수 템플릿으로 복사 생성자를 작성 - 타입 U*에서 타입 T*로 암시적 변환이 가능할 때만 컴파일이 되도록 작동한다
  • 8. shared_ptr 의 멤버 함수 템플릿 호환되는 모든 기본 제공 포인터 생성자 shared_ptr, weak_ptr 에 대응하는 생성자 shared_ptr 을 통한 암시적 변환만 허용하고 있다
  • 9. shared_ptr 의 멤버 함수 템플릿 - template <bool B, class T = void> struct enable_if - template <class From, class To> struct is_convertible - unique_ptr 대입 생성자 - unique_ptr 은 shared_ptr에 이동되기 때문에 const 키워드가 빠져있고 오른값으로 들어온다
  • 10. shared_ptr 의 멤버 함수 템플릿 shared_ptr, unique_ptr 대입 연산자
  • 11. 기본 복사 생성자, 일반화 복사 생성자 일반화 복사 생성자가 있어도 호출 되지 않는다
  • 12. 기본 복사 생성자, 일반화 복사 생성자 일반화 복사 생성자가 있어도 기본 생성자는 생성됨을 알 수 있다
  • 13. 이것만은 잊지 말자! - 호환되는 모든 타입을 받아들이는 멤버 함수를 만들려면 멤버 함수 템플릿을 사용합시다 - 일반화된 복사 생성 연산과 일반화된 대입 연산을 위해 멤버 템플릿을 선언했다 하더라도, 보통의 복사 생성자와 복사 대입 연산자는 여전히 직접 선언해야 합니다