// C++ program to illustrate unary leftfold expression #include using namespace std; //Unary Left Fold: //Syntax: (... op pack) //Expansion: (((E1 op E2) op ...) op EN) // sum_right(1, 2, 3) expands to ((1 + 2) + 3)) template auto sum_left_fold_unary(Args... args) { return (... + args); } //Unary Right Fold // ( 1 + ( 2 + 3 ) ) template auto sum_right_fold_unary(Args... args) { return (args + ...); // Unary Right Fold with addition } // (((value + 3 ) + 1 ) + 2 ) //sum_left_fold_binary( 3, 1, 2 ) template auto sum_left_fold_binary( T initial_value , Args... args ) { return (initial_value + ... + args); } // (((value + 3 ) + 1 ) + 2 ) //sum_left_fold_binary( 3, 1, 2 ) // ( value + (3 + (1 + 2) ) ) template auto sum_right_fold_binary( T initial_value , Args... args ) { return (initial_value + ... + args); } // Function template that accepts a variadic number of arguments template void print_to_cout(Args&&... args) { //binary operator // This expands to (cout << arg1 << arg2 << ... << argN) ( cout << ... << forward(args) ); cout << '\n'; // Add a newline at the end } int main() { int sum1 = sum_left_fold_unary( 3, 1, 2 ) ; cout << "Result: " << sum1 << endl; int sum2 = sum_right_fold_unary( 3, 1, 2 ) ; cout << "Result: " << sum2 << endl; int sum3 = sum_left_fold_binary( 3, 1, 2 ) ; cout << "Result: " << sum3 << endl; int sum4 = sum_right_fold_binary( 3, 1, 2 ) ; cout << "Result: " << sum4 << endl; print_to_cout("Hello", " ", "World", "!") ; print_to_cout(1, 2.5, 'c', "string_literal") ; return 0; }