C++17 Folding expressions

Folding expressions
Variadic Template(C++ 11)
재귀 함수 호출을 종료시켜주는 코드가
필요하다
Sum(T t) 함수가 가독성도 떨어지고 생
산성도 떨어지기 때문에 C++ 17에
Folding expressions 이 나옴
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4191.html
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4295.html
재귀 함수 호출을 종료시켜주는 코드가
없어도 됨
+ - * / % ^ & | = < > << >> += -= *=
/= %= ^= &= |= <<= >>= == != <= >= &&
|| , .* ->*
연산자 사용가능
https://en.cppreference.com/w/cpp/language/fold
unary right fold(단항 오른쪽 접기)
(E op ...) -> (E1 op (... op (EN-1 op EN)))
입력 : fun(1, 2, 3, 4, 5), 표현식 : t + …
(1 + (2 + (3 + (4 + 5)))
unary left fold(단항 왼쪽 접기)
(... op E) -> (((E1 op E2) op ...) op EN)
입력 : fun(1, 2, 3, 4, 5), 표현식 : … + t
((((1 + 2) + 3) +4) + 5)
unary fold-expression
print("단");print("짠");
과 같다
unary fold-expression
N이 0일 경우 기본값은 아래와 같이 들어가며 저기에 들어갈 수 없는 타입일 경우
에러가 난다
binary right fold(이항 오른쪽 접기)
(E op ... op I) -> (E1 op (... op (EN−1 op (EN op I))))
func1(3, 5, 7, 9)
->(3 + (5 + (7 + (9 + 1/*init*/))))
binary left fold(이항 왼쪽 접기)
(I op ... op E) -> ((((I op E1) op E2) op ...) op EN)
func2(3, 5, 7, 9)
->((((1/*init*/ + 3) + 5) + 7) + 9)
binary fold
binary fold
cast 보다 우선순위가 낮은 연산자가 있으면 안됨
()로 묶으면 해결된다
binary fold
1 de 10

Recomendados

C++17 Key Features Summary - Ver 2 por
C++17 Key Features Summary - Ver 2C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2Chris Ohk
17.6K vistas83 diapositivas
불어오는 변화의 바람, From c++98 to c++11, 14 por
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 명신 김
160 vistas54 diapositivas
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호) por
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)Sang Don Kim
2.7K vistas79 diapositivas
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기 por
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기Chris Ohk
6.8K vistas79 diapositivas
[C++ Korea 2nd Seminar] C++17 Key Features Summary por
[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
10.5K vistas79 diapositivas
[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C# 혼합 멀티플랫폼 게임 아키텍처 설계 por
[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C#  혼합 멀티플랫폼 게임 아키텍처 설계[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C#  혼합 멀티플랫폼 게임 아키텍처 설계
[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C# 혼합 멀티플랫폼 게임 아키텍처 설계Sungkyun Kim
17.4K vistas48 diapositivas

Más contenido relacionado

Similar a C++17 Folding expressions

C Language I por
C Language IC Language I
C Language ISuho Kwon
199 vistas318 diapositivas
Changes in c++0x por
Changes in c++0xChanges in c++0x
Changes in c++0xSang Yeon Jeon
682 vistas18 diapositivas
Ch05 por
Ch05Ch05
Ch05Hankyo
766 vistas65 diapositivas
Modern C++의 타입 추론과 람다, 컨셉 por
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉HyunJoon Park
205 vistas40 diapositivas
함수적 사고 2장 por
함수적 사고 2장함수적 사고 2장
함수적 사고 2장HyeonSeok Choi
663 vistas24 diapositivas
포트폴리오에서 사용한 모던 C++ por
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++KWANGIL KIM
673 vistas53 diapositivas

Similar a C++17 Folding expressions(19)

C Language I por Suho Kwon
C Language IC Language I
C Language I
Suho Kwon199 vistas
Ch05 por Hankyo
Ch05Ch05
Ch05
Hankyo766 vistas
Modern C++의 타입 추론과 람다, 컨셉 por HyunJoon Park
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉
HyunJoon Park205 vistas
포트폴리오에서 사용한 모던 C++ por KWANGIL KIM
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++
KWANGIL KIM673 vistas
Deview 2019 눈발자국 por hanbeom Park
Deview 2019 눈발자국Deview 2019 눈발자국
Deview 2019 눈발자국
hanbeom Park44 vistas
[C++ Korea] Effective Modern C++ Study, Item 1 - 3 por Chris Ohk
[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
Chris Ohk3K vistas
14장 - 15장 예외처리, 템플릿 por 유석 남
14장 - 15장 예외처리, 템플릿14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿
유석 남726 vistas
RNC C++ lecture_2 Variable DataType por itlockit
RNC C++ lecture_2 Variable DataTypeRNC C++ lecture_2 Variable DataType
RNC C++ lecture_2 Variable DataType
itlockit33 vistas
Lambda 란 무엇인가 por Vong Sik Kong
Lambda 란 무엇인가Lambda 란 무엇인가
Lambda 란 무엇인가
Vong Sik Kong6.3K vistas
프로그래밍 및 실습 Chap2 por dktm
프로그래밍 및 실습 Chap2프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2
dktm2.5K vistas
[C++ Korea] Effective Modern C++ Study item14 16 +신촌 por Seok-joon Yun
[C++ Korea] Effective Modern C++ Study item14 16 +신촌[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
Seok-joon Yun2.8K vistas
[shaderx6]8.2 3d engine tools with c++cli por 종빈 오
[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
종빈 오562 vistas
DEVIEW-FULL-감독판.pptx por hanbeom Park
DEVIEW-FULL-감독판.pptxDEVIEW-FULL-감독판.pptx
DEVIEW-FULL-감독판.pptx
hanbeom Park9 vistas
모던 C++ 정리 por Hansol Kang
모던 C++ 정리모던 C++ 정리
모던 C++ 정리
Hansol Kang149 vistas
NDC 2011, 네트워크 비동기 통신, 합의점의 길목에서 por tcaesvk
NDC 2011, 네트워크 비동기 통신, 합의점의 길목에서NDC 2011, 네트워크 비동기 통신, 합의점의 길목에서
NDC 2011, 네트워크 비동기 통신, 합의점의 길목에서
tcaesvk991 vistas

Más de 진화 손

C++ 20 class template argument deduction for alias templates por
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진화 손
3 vistas3 diapositivas
C++ 20 Make stateful allocator propagation more consistent for operator+(basi... por
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...진화 손
5 vistas5 diapositivas
C++ 20 Unevaluated asm-declaration in constexpr functions por
C++ 20 Unevaluated asm-declaration in constexpr functionsC++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functions진화 손
4 vistas3 diapositivas
C++20 Utility functions to implement uses-allocator construction.pdf por
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진화 손
7 vistas9 diapositivas
C++ 20 std__reference_wrapper for incomplete types por
C++ 20 std__reference_wrapper for incomplete typesC++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete types진화 손
8 vistas2 diapositivas
C++ 20 Stronger Unicode requirements por
C++ 20 Stronger Unicode requirementsC++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirements진화 손
5 vistas2 diapositivas

Más de 진화 손(20)

C++ 20 class template argument deduction for alias templates por 진화 손
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
진화 손3 vistas
C++ 20 Make stateful allocator propagation more consistent for operator+(basi... por 진화 손
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...
진화 손5 vistas
C++ 20 Unevaluated asm-declaration in constexpr functions por 진화 손
C++ 20 Unevaluated asm-declaration in constexpr functionsC++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functions
진화 손4 vistas
C++20 Utility functions to implement uses-allocator construction.pdf por 진화 손
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
진화 손7 vistas
C++ 20 std__reference_wrapper for incomplete types por 진화 손
C++ 20 std__reference_wrapper for incomplete typesC++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete types
진화 손8 vistas
C++ 20 Stronger Unicode requirements por 진화 손
C++ 20 Stronger Unicode requirementsC++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirements
진화 손5 vistas
C++20 Concepts library por 진화 손
C++20 Concepts libraryC++20 Concepts library
C++20 Concepts library
진화 손3 vistas
C++20 Coroutine por 진화 손
C++20 CoroutineC++20 Coroutine
C++20 Coroutine
진화 손50 vistas
C++ 20 Relaxing the range-for loop customization point finding rules por 진화 손
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
진화 손6 vistas
C++ 20 Relaxing the structured bindings customization point finding rules por 진화 손
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
진화 손7 vistas
C++20 explicit(bool) por 진화 손
C++20 explicit(bool)C++20 explicit(bool)
C++20 explicit(bool)
진화 손6 vistas
C++20 std::map::contains por 진화 손
C++20 std::map::containsC++20 std::map::contains
C++20 std::map::contains
진화 손5 vistas
C++20 Comparing unordered containers por 진화 손
C++20 Comparing unordered containersC++20 Comparing unordered containers
C++20 Comparing unordered containers
진화 손8 vistas
C++20 Attributes [[likely]] and [[unlikely]] por 진화 손
C++20 Attributes [[likely]] and [[unlikely]]C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]
진화 손21 vistas
C++ 20 Lambdas in unevaluated contexts por 진화 손
C++ 20 Lambdas in unevaluated contextsC++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contexts
진화 손4 vistas
C++20 Library support for operator<=> <compare> por 진화 손
C++20 Library support for operator<=> <compare>C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>
진화 손2 vistas
C++20 const mismatch with defaulted copy constructor por 진화 손
C++20 const mismatch with defaulted copy constructorC++20 const mismatch with defaulted copy constructor
C++20 const mismatch with defaulted copy constructor
진화 손5 vistas
C++20 Atomic std::shared_ptr and std::weak_ptr por 진화 손
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
진화 손11 vistas
C++20 Default member initializers for bit-fields por 진화 손
C++20 Default member initializers for bit-fieldsC++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fields
진화 손10 vistas
C++ 20 constexpr for algorithm and utility por 진화 손
C++ 20 constexpr for  algorithm and utilityC++ 20 constexpr for  algorithm and utility
C++ 20 constexpr for algorithm and utility
진화 손8 vistas

C++17 Folding expressions

  • 2. Variadic Template(C++ 11) 재귀 함수 호출을 종료시켜주는 코드가 필요하다 Sum(T t) 함수가 가독성도 떨어지고 생 산성도 떨어지기 때문에 C++ 17에 Folding expressions 이 나옴 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4191.html https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4295.html
  • 3. 재귀 함수 호출을 종료시켜주는 코드가 없어도 됨 + - * / % ^ & | = < > << >> += -= *= /= %= ^= &= |= <<= >>= == != <= >= && || , .* ->* 연산자 사용가능 https://en.cppreference.com/w/cpp/language/fold
  • 4. unary right fold(단항 오른쪽 접기) (E op ...) -> (E1 op (... op (EN-1 op EN))) 입력 : fun(1, 2, 3, 4, 5), 표현식 : t + … (1 + (2 + (3 + (4 + 5))) unary left fold(단항 왼쪽 접기) (... op E) -> (((E1 op E2) op ...) op EN) 입력 : fun(1, 2, 3, 4, 5), 표현식 : … + t ((((1 + 2) + 3) +4) + 5)
  • 6. unary fold-expression N이 0일 경우 기본값은 아래와 같이 들어가며 저기에 들어갈 수 없는 타입일 경우 에러가 난다
  • 7. binary right fold(이항 오른쪽 접기) (E op ... op I) -> (E1 op (... op (EN−1 op (EN op I)))) func1(3, 5, 7, 9) ->(3 + (5 + (7 + (9 + 1/*init*/)))) binary left fold(이항 왼쪽 접기) (I op ... op E) -> ((((I op E1) op E2) op ...) op EN) func2(3, 5, 7, 9) ->((((1/*init*/ + 3) + 5) + 7) + 9)
  • 9. binary fold cast 보다 우선순위가 낮은 연산자가 있으면 안됨 ()로 묶으면 해결된다