SlideShare a Scribd company logo
1 of 17
Download to read offline
Effective Modern C++ 스터디
item 25.
오른값 참조에는 std::move 를,
보편 참조에는 std::forward를
사용하라
이데아 게임즈 손진화
move 용도
오른값 참조를 다른 함수로 전달할 때 사용
forward 용도
조건부 캐스팅이 필요할 때 사용
잘못된 사용 예 1
오른값 참조에 forward 사용
n은 미지정 값을
가지게 된다
잘못된 사용 예 1
오버로딩을 사용해서 해결?
잘못된 사용 예 1
오버로딩을 사용해서 해결?
+ 관리 해야 할 코드 증가
+ 수행 비용 증가
+ 가변성이 나빠진다
잘못된 사용 예 2
move나 forward로 보낸 객체를 사용
순서가 바뀌면 의도하지 않은 결과가 나올 수 있다
적절한 사용 예
함수 리턴 값이 value 타입이고
반환형식이 move나 forward에 묶여있는 경우
적절한 사용 예
함수 리턴 값이 value 타입이고
반환형식이 move나 forward에 묶여있는 경우
이동 연산이 복사 연산보다
비용이 적을 경우
성능상 이득을 얻을 수 있다
잘못된 사용 예 3
함수 리턴 값이 value 타입이고
반환형식이 이름이 있는 지역 변수일 경우
반환값 최적화
Return Value Optimization
- 그 지역 객체의 형식이
함수의 반환 형식과 같고
- 그 지역 객체가 함수의 반환 값일 경우
임시객체의 생성을 피하도록 최적화 한다
반환값 최적화
임시객체가 생성 된다
반환값 최적화
임시객체가 생성되지 않는다
반환값 최적화
속성 -> C/C++ -> 최적화 -> 속도 최대화(/O2)
임시객체가 생성되지 않는다
잘못된 사용 예 3
함수 리턴 값이 value 타입이고
반환형식이 이름이 있는 지역 변수일 경우
임시객체가 생성 된다
결론
- 오른값 참조나 보편 참조가 마지막으로 쓰이
는 지점에서, 오른값 참조에는 std::move를,
보편 참조에는 std::forward를 적용하라
- 결과를 값 전달방식으로 돌려주는 함수가 오
른값 참조나 보편 참조를 돌려줄 때에도 각각
std::move나 std::forward를 적용하라
결론
- 반환값 최적화의 대상이 될 수 있는 지역 객체
에는 절대로 std::move나 std::forward를 적
용하지 말아야 한다

More Related Content

More from 진화 손

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진화 손
 
C++ 20 constexpr for algorithm and utility
C++ 20 constexpr for  algorithm and utilityC++ 20 constexpr for  algorithm and utility
C++ 20 constexpr for algorithm and utility진화 손
 
C++17 init-statements for if and switch
C++17 init-statements for if and switchC++17 init-statements for if and switch
C++17 init-statements for if and switch진화 손
 
C++17 Aggregate classes with base classes
C++17 Aggregate classes with base classesC++17 Aggregate classes with base classes
C++17 Aggregate classes with base classes진화 손
 
C++17 std::byte
C++17 std::byteC++17 std::byte
C++17 std::byte진화 손
 

More from 진화 손 (20)

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
 
C++ 20 constexpr for algorithm and utility
C++ 20 constexpr for  algorithm and utilityC++ 20 constexpr for  algorithm and utility
C++ 20 constexpr for algorithm and utility
 
C++17 init-statements for if and switch
C++17 init-statements for if and switchC++17 init-statements for if and switch
C++17 init-statements for if and switch
 
C++17 Aggregate classes with base classes
C++17 Aggregate classes with base classesC++17 Aggregate classes with base classes
C++17 Aggregate classes with base classes
 
C++17 std::byte
C++17 std::byteC++17 std::byte
C++17 std::byte
 
C++17 not_fn
C++17 not_fnC++17 not_fn
C++17 not_fn
 

Effective modern cpp item25