-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-fold.cc
43 lines (37 loc) · 829 Bytes
/
02-fold.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// Program
// Fold expression: associativity test in fold expression
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o 02-fold 02-fold.cc
//
// Execution
// ./02-fold
//
#include <iostream>
// right associativity
template <typename ... Args>
auto subtract_1(Args ... args) {
return (args - ... - 0);
}
// left associativity
template <typename ... Args>
auto subtract_2(Args ... args) {
return (0 - ... - args);
}
//
// Entry function
//
int main() {
// associativity
{
std::cout << " 1 - (2 - 3) = " << subtract_1(1, 2, 3) << '\n'; // ans is 2
std::cout << "(1 - 2) - 3 = " << subtract_2(1, 2, 3) << '\n'; // ans is -4
}
// no argument
{
std::cout << " ... = " << subtract_1() << '\n'; // ans is 0
std::cout << " ... = " << subtract_2() << '\n'; // ans is 0
}
return 0;
}