SlideShare a Scribd company logo
1 of 18
Download to read offline
Item 41.
이동이 저렴하고 항상 복사되는
복사 가능 매개변수에 대해서는
값 전달을 고려하라
Effective Modern C++ 스터디
이데아 게임즈 손진화
발단
위의 함수를 오른값, 왼값을 다 처리 할 수
있도록 만들고 싶다
방법 1
오버로딩을 이용하여 해결
방법 1 - 단점
- 관리 해야 할 코드가 늘어남
- 목적코드가 늘어남
방법 2
보편참조 + 함수 템플릿 를 이용하여 해결
방법 2 - 단점
- 템플릿이기 때문에 구현코드를 항상
헤더파일에 작성해야 한다
- 목적 코드에는 이 템플릿의 서로 다른
인스턴스가 여러 개 포함될 수 있다
widget.addName("ddd");
widget.addName(string("ddddd"));
방법 2 - 단점
- 보편 참조로는 전달할 수 없는 타입이 존재
nullptr, 중괄호 이니셜라이저, 비트필드,
선언만 된 정수 static const 및 constexpr 자료 멤버,
오버로딩된 함수 이름과 템플릿 이름
- 허용되지 않는 타입을 전달했을 경우
오류메시지를 이해하기 힘들다
방법 3
값으로 전달
앞에서 나온 단점들이 전부 해결됨
하지만 비용은?
비용 비교
방법1 - 중복적재
Widget w;
String name(“Bart”);
w.addName(name); // 복사 1회
w.addName(name + “Jenne”); // 이동 1회
비용 비교
방법2 – 보편 참조
Widget w;
String name(“Bart”);
w.addName(name); // 복사 1회
w.addName(name + “Jenne”); // 이동 1회
비용 비교
방법3 – 값 전달
Widget w;
String name(“Bart”);
w.addName(name); // 복사 1회, 이동 1회
w.addName(name + “Jenne”); // 이동 2회
이동이 저렴하고 항상 복사되는
복사 가능 매개변수에 대해서는
값 전달을 고려하라
이동 비용이 싸고
복사 가능한 타입이고
전달한 값을 함수 내 에서 항상 복사한다면
방법3을 생각 해 볼만 하다
방법3을 선택할 때 주의할 점
- 다른 방법들에 비해 비용이 크다
- 복사 가능 매개변수에 대해서만
값 전달을 고려해야 한다
이동 전용 매개변수는 이동용 함수 하나만 있으면 된다
widget.setPtr("Morden C++");
void setPtr(unique_ptr<string>&& ptr ) // 이동 1회
void setPtr(unique_ptr<string> ptr ) // 이동 2회
방법3을 선택할 때 주의할 점
- 이동 비용이 저렴해야한다
다른 방법에 비해 이동 횟수가 1회 늘었기 때문
- 항상 복사되어야 한다
복사가 되지 않는다면 쓸데없이 생성자만 호출 됨
방법3을 선택할 때 주의할 점
- 동적 할당에 의존하는 타입은 적절하지 않다
std::string, std::vector
위 함수는 text가 newPwd보다 길이가 작을 때만 메모리
재할당과 해제가 일어나지만 아래함수는 항상 발생한다
방법3을 선택할 때 주의할 점
- 잘림 문제
파생 클래스를 베이스 클래스에 할당하면 베이스 클래스에
해당되는 값들은 복사되지만 확장된 값들은 저장할 곳이
없어서 복사되지 않는 현상
C++11에서도 여전히 존재
class Widget {…};
class SpecialWidget : public Widget {…};
void processWidget(Widget w);
SpecialWidget sw;
processWidget(sw); // Widget 으로 인식됨!
기억해 둘 사항들
- 이동이 저렴하고 항상 복사되는 복사 가능
매개변수에 대해서는 값 전달이 참조 전달
만큼이나 효율적이고, 구현하기가 더 쉽고,
산출되는 목적 코드의 크기도 더 작다
- 왼값 인수의 경우 값 전달(복사 생성) 다음의
이동 배정은 참조 전달 다음의 복사 배정보다
훨씬 비쌀 가능성이 있다
기억해 둘 사항들
- 값 전달에서는 잘림 문제가 발생할 수 있으므
로, 일반적으로 기반 클래스 매개 변수 형식에
대해서는 값 전달이 적합하지 않다

More Related Content

Similar to Effective modern cpp item41

More effective c++ 챕터3~4ppt
More effective c++ 챕터3~4pptMore effective c++ 챕터3~4ppt
More effective c++ 챕터3~4pptInjae Lee
 
Refactoring tutorial
Refactoring tutorialRefactoring tutorial
Refactoring tutorialBingu Shim
 
Refactoring Tutorial 1주차[ Refactoring 개요]
Refactoring  Tutorial 1주차[ Refactoring 개요]Refactoring  Tutorial 1주차[ Refactoring 개요]
Refactoring Tutorial 1주차[ Refactoring 개요]Bingu Shim
 
Refactoring Tutorial 1주차[ Refactoring 개요]
Refactoring  Tutorial 1주차[ Refactoring 개요]Refactoring  Tutorial 1주차[ Refactoring 개요]
Refactoring Tutorial 1주차[ Refactoring 개요]Bingu Shim
 
Refactoring tutorial 1주차[refactoring 개요]
Refactoring tutorial 1주차[refactoring 개요]Refactoring tutorial 1주차[refactoring 개요]
Refactoring tutorial 1주차[refactoring 개요]bbongcsu
 
[15]Android Kotlin을 통한 개발 전략
[15]Android Kotlin을 통한 개발 전략[15]Android Kotlin을 통한 개발 전략
[15]Android Kotlin을 통한 개발 전략NAVER Engineering
 
Effective c++chapter4
Effective c++chapter4Effective c++chapter4
Effective c++chapter4성연 김
 
DevRookie 리펙터링.pptx
DevRookie 리펙터링.pptxDevRookie 리펙터링.pptx
DevRookie 리펙터링.pptxMUUMUMUMU
 
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
 
Machine learning linearregression
Machine learning linearregressionMachine learning linearregression
Machine learning linearregressionHaYoungChoi17
 
NDC11_김성익_슈퍼클래스
NDC11_김성익_슈퍼클래스NDC11_김성익_슈퍼클래스
NDC11_김성익_슈퍼클래스Sungik Kim
 
Objective-C Runtime Programming Guide
Objective-C Runtime Programming GuideObjective-C Runtime Programming Guide
Objective-C Runtime Programming GuideSung-Kwan Kim
 
[shaderx6]8.2 3d engine tools with c++cli
[shaderx6]8.2 3d engine tools with c++cli[shaderx6]8.2 3d engine tools with c++cli
[shaderx6]8.2 3d engine tools with c++cli종빈 오
 
Legacy code refactoring video rental system
Legacy code refactoring   video rental systemLegacy code refactoring   video rental system
Legacy code refactoring video rental systemJaehoon Oh
 

Similar to Effective modern cpp item41 (16)

More effective c++ 챕터3~4ppt
More effective c++ 챕터3~4pptMore effective c++ 챕터3~4ppt
More effective c++ 챕터3~4ppt
 
Refactoring tutorial
Refactoring tutorialRefactoring tutorial
Refactoring tutorial
 
Refactoring Tutorial 1주차[ Refactoring 개요]
Refactoring  Tutorial 1주차[ Refactoring 개요]Refactoring  Tutorial 1주차[ Refactoring 개요]
Refactoring Tutorial 1주차[ Refactoring 개요]
 
Refactoring Tutorial 1주차[ Refactoring 개요]
Refactoring  Tutorial 1주차[ Refactoring 개요]Refactoring  Tutorial 1주차[ Refactoring 개요]
Refactoring Tutorial 1주차[ Refactoring 개요]
 
Refactoring tutorial 1주차[refactoring 개요]
Refactoring tutorial 1주차[refactoring 개요]Refactoring tutorial 1주차[refactoring 개요]
Refactoring tutorial 1주차[refactoring 개요]
 
[15]Android Kotlin을 통한 개발 전략
[15]Android Kotlin을 통한 개발 전략[15]Android Kotlin을 통한 개발 전략
[15]Android Kotlin을 통한 개발 전략
 
Effective c++chapter4
Effective c++chapter4Effective c++chapter4
Effective c++chapter4
 
DevRookie 리펙터링.pptx
DevRookie 리펙터링.pptxDevRookie 리펙터링.pptx
DevRookie 리펙터링.pptx
 
Effective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshinEffective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshin
 
Machine learning linearregression
Machine learning linearregressionMachine learning linearregression
Machine learning linearregression
 
8.다중메서드
8.다중메서드8.다중메서드
8.다중메서드
 
NDC11_김성익_슈퍼클래스
NDC11_김성익_슈퍼클래스NDC11_김성익_슈퍼클래스
NDC11_김성익_슈퍼클래스
 
Objective-C Runtime Programming Guide
Objective-C Runtime Programming GuideObjective-C Runtime Programming Guide
Objective-C Runtime Programming Guide
 
[shaderx6]8.2 3d engine tools with c++cli
[shaderx6]8.2 3d engine tools with c++cli[shaderx6]8.2 3d engine tools with c++cli
[shaderx6]8.2 3d engine tools with c++cli
 
MEC++ 3,4
MEC++ 3,4MEC++ 3,4
MEC++ 3,4
 
Legacy code refactoring video rental system
Legacy code refactoring   video rental systemLegacy code refactoring   video rental system
Legacy code refactoring video rental system
 

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 modern cpp item41

  • 1. Item 41. 이동이 저렴하고 항상 복사되는 복사 가능 매개변수에 대해서는 값 전달을 고려하라 Effective Modern C++ 스터디 이데아 게임즈 손진화
  • 2. 발단 위의 함수를 오른값, 왼값을 다 처리 할 수 있도록 만들고 싶다
  • 4. 방법 1 - 단점 - 관리 해야 할 코드가 늘어남 - 목적코드가 늘어남
  • 5. 방법 2 보편참조 + 함수 템플릿 를 이용하여 해결
  • 6. 방법 2 - 단점 - 템플릿이기 때문에 구현코드를 항상 헤더파일에 작성해야 한다 - 목적 코드에는 이 템플릿의 서로 다른 인스턴스가 여러 개 포함될 수 있다 widget.addName("ddd"); widget.addName(string("ddddd"));
  • 7. 방법 2 - 단점 - 보편 참조로는 전달할 수 없는 타입이 존재 nullptr, 중괄호 이니셜라이저, 비트필드, 선언만 된 정수 static const 및 constexpr 자료 멤버, 오버로딩된 함수 이름과 템플릿 이름 - 허용되지 않는 타입을 전달했을 경우 오류메시지를 이해하기 힘들다
  • 8. 방법 3 값으로 전달 앞에서 나온 단점들이 전부 해결됨 하지만 비용은?
  • 9. 비용 비교 방법1 - 중복적재 Widget w; String name(“Bart”); w.addName(name); // 복사 1회 w.addName(name + “Jenne”); // 이동 1회
  • 10. 비용 비교 방법2 – 보편 참조 Widget w; String name(“Bart”); w.addName(name); // 복사 1회 w.addName(name + “Jenne”); // 이동 1회
  • 11. 비용 비교 방법3 – 값 전달 Widget w; String name(“Bart”); w.addName(name); // 복사 1회, 이동 1회 w.addName(name + “Jenne”); // 이동 2회
  • 12. 이동이 저렴하고 항상 복사되는 복사 가능 매개변수에 대해서는 값 전달을 고려하라 이동 비용이 싸고 복사 가능한 타입이고 전달한 값을 함수 내 에서 항상 복사한다면 방법3을 생각 해 볼만 하다
  • 13. 방법3을 선택할 때 주의할 점 - 다른 방법들에 비해 비용이 크다 - 복사 가능 매개변수에 대해서만 값 전달을 고려해야 한다 이동 전용 매개변수는 이동용 함수 하나만 있으면 된다 widget.setPtr("Morden C++"); void setPtr(unique_ptr<string>&& ptr ) // 이동 1회 void setPtr(unique_ptr<string> ptr ) // 이동 2회
  • 14. 방법3을 선택할 때 주의할 점 - 이동 비용이 저렴해야한다 다른 방법에 비해 이동 횟수가 1회 늘었기 때문 - 항상 복사되어야 한다 복사가 되지 않는다면 쓸데없이 생성자만 호출 됨
  • 15. 방법3을 선택할 때 주의할 점 - 동적 할당에 의존하는 타입은 적절하지 않다 std::string, std::vector 위 함수는 text가 newPwd보다 길이가 작을 때만 메모리 재할당과 해제가 일어나지만 아래함수는 항상 발생한다
  • 16. 방법3을 선택할 때 주의할 점 - 잘림 문제 파생 클래스를 베이스 클래스에 할당하면 베이스 클래스에 해당되는 값들은 복사되지만 확장된 값들은 저장할 곳이 없어서 복사되지 않는 현상 C++11에서도 여전히 존재 class Widget {…}; class SpecialWidget : public Widget {…}; void processWidget(Widget w); SpecialWidget sw; processWidget(sw); // Widget 으로 인식됨!
  • 17. 기억해 둘 사항들 - 이동이 저렴하고 항상 복사되는 복사 가능 매개변수에 대해서는 값 전달이 참조 전달 만큼이나 효율적이고, 구현하기가 더 쉽고, 산출되는 목적 코드의 크기도 더 작다 - 왼값 인수의 경우 값 전달(복사 생성) 다음의 이동 배정은 참조 전달 다음의 복사 배정보다 훨씬 비쌀 가능성이 있다
  • 18. 기억해 둘 사항들 - 값 전달에서는 잘림 문제가 발생할 수 있으므 로, 일반적으로 기반 클래스 매개 변수 형식에 대해서는 값 전달이 적합하지 않다