// File: iZhangHui[P]CppPrimer/CH14_OverloadedOperationsAndConversions/examples/fcnobj.cc
// Function: main
int main() {

	cout << plus<int>()(3,4) << endl; // prints 7

	plus<int> intAdd;      // object that can add two int values
	negate<int> intNegate; // object that can negate an int value

	// uses intAdd::operator(int, int) to add 10 and 20
	int sum = intAdd(10, 20);         // equivalent to sum = 30
	cout << sum << endl;

	sum = intNegate(intAdd(10, 20));  // equivalent to sum = -30
	cout << sum << endl;

	// uses intNegate::operator(int) to generate -10
	// as the second argument to intAdd::operator(int, int)
	sum = intAdd(10, intNegate(10));  // sum = 0

	cout << sum << endl;

	vector<int> vec = {0,1,2,3,4,5,16,17,18,19};

	// bind second argument to less_equal
	// 0,1,2,3,4,5
	cout << count_if(vec.begin(), vec.end(),
		             bind(less_equal<int>(), _1, 10));
	cout << endl;

	vector<string> svec;
	string in;
	while (cin >> in)
		svec.push_back(in);

	// function<bool(const string&, string::size_type)> fp1 = size_compare;
	function<decltype(size_compare)> fp1 = size_compare;

	// decltype(fp1)::result_type ret;
	// function<bool(const string&)> fp2 = bind(size_compare, _1, 6);
	function<bool(const string&)> fp2 = bind(fp1, _1, 6);

	cout << count_if(svec.begin(), svec.end(), fp2)
	     << endl;
	cout << count_if(svec.begin(), svec.end(),
	                 bind(size_compare, _1, 6))
	     << endl;


	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movl $0x0, -0x4(%rbp)
movl $0x3, -0xc(%rbp)
movl $0x4, -0x10(%rbp)
leaq -0x5(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x10(%rbp), %rdx
callq 0x2910
movl %eax, %esi
movq 0x8c66(%rip), %rdi      # 0xafd0
movq %rdi, -0x1e0(%rbp)
callq 0x2190
movq %rax, %rdi
movq 0x8c38(%rip), %rsi      # 0xafb8
movq %rsi, -0x1d8(%rbp)
callq 0x2110
movl $0xa, -0x1c(%rbp)
movl $0x14, -0x20(%rbp)
leaq -0x11(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
leaq -0x1c(%rbp), %rsi
leaq -0x20(%rbp), %rdx
callq 0x2910
movq -0x1e0(%rbp), %rdi
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %esi
callq 0x2190
movq -0x1d8(%rbp), %rsi
movq %rax, %rdi
callq 0x2110
movq -0x1e8(%rbp), %rdi
movl $0xa, -0x28(%rbp)
movl $0x14, -0x2c(%rbp)
leaq -0x28(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
callq 0x2910
movl %eax, -0x24(%rbp)
leaq -0x12(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
leaq -0x24(%rbp), %rsi
callq 0x2930
movq -0x1e0(%rbp), %rdi
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %esi
callq 0x2190
movq -0x1d8(%rbp), %rsi
movq %rax, %rdi
callq 0x2110
movq -0x1f0(%rbp), %rdi
movl $0xa, -0x30(%rbp)
movl $0xa, -0x38(%rbp)
leaq -0x38(%rbp), %rsi
callq 0x2930
movq -0x1e8(%rbp), %rdi
movl %eax, -0x34(%rbp)
leaq -0x30(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x2910
movq -0x1e0(%rbp), %rdi
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %esi
callq 0x2190
movq -0x1d8(%rbp), %rsi
movq %rax, %rdi
callq 0x2110
movabsq $0x1300000012, %rax     # imm = 0x1300000012
movq %rax, -0x70(%rbp)
movups 0x3b9c(%rip), %xmm0     # 0x6034
movaps %xmm0, -0x80(%rbp)
movups 0x3b81(%rip), %xmm0     # 0x6024
movaps %xmm0, -0x90(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0xa, -0x58(%rbp)
leaq -0x91(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x2950
movq -0x1d0(%rbp), %rcx
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
leaq -0x50(%rbp), %rdi
callq 0x2970
jmp 0x24ea
leaq -0x91(%rbp), %rdi
callq 0x29f0
leaq -0x50(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x2a60
movq -0x1f8(%rbp), %rdi
movq %rax, -0xb0(%rbp)
callq 0x2a90
movq %rax, -0xb8(%rbp)
movl $0xa, -0xc8(%rbp)
movq 0x8a97(%rip), %rdx      # 0xafc8
leaq -0xc0(%rbp), %rdi
leaq -0xc1(%rbp), %rsi
leaq -0xc8(%rbp), %rcx
callq 0x2ac0
jmp 0x254d
movq -0xb0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
callq 0x2a10
movq %rax, -0x200(%rbp)
jmp 0x2570
movq -0x200(%rbp), %rsi
movq 0x8a52(%rip), %rdi      # 0xafd0
callq 0x21b0
jmp 0x2585
movq 0x8a44(%rip), %rdi      # 0xafd0
movq 0x8a25(%rip), %rsi      # 0xafb8
callq 0x2110
jmp 0x259a
leaq -0xe0(%rbp), %rdi
callq 0x2b00
leaq -0x100(%rbp), %rdi
callq 0x2140
movq 0x8a27(%rip), %rdi      # 0xafe0
leaq -0x100(%rbp), %rsi
callq 0x2130
movq %rax, -0x208(%rbp)
jmp 0x25ce
movq -0x208(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2120
movb %al, -0x209(%rbp)
jmp 0x25ec
movb -0x209(%rbp), %al
testb $0x1, %al
jne 0x25f8
jmp 0x2660
leaq -0xe0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x2b20
jmp 0x260d
jmp 0x25b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x91(%rbp), %rdi
callq 0x29f0
jmp 0x2903
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x28fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x28e2
leaq -0x367(%rip), %rsi      # 0x2300
leaq -0x120(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x2b90
movq -0x218(%rbp), %rsi
movl $0x6, -0x16c(%rbp)
movq 0x8936(%rip), %rdx      # 0xafc8
leaq -0x168(%rbp), %rdi
leaq -0x16c(%rbp), %rcx
callq 0x2c20
jmp 0x26a7
leaq -0x140(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x2c60
jmp 0x26bc
leaq -0x168(%rbp), %rdi
callq 0x2d00
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x2df0
movq -0x220(%rbp), %rdi
movq %rax, -0x178(%rbp)
callq 0x2e20
movq %rax, -0x180(%rbp)
leaq -0x1a0(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x2e50
jmp 0x270a
movq -0x178(%rbp), %rdi
movq -0x180(%rbp), %rsi
leaq -0x1a0(%rbp), %rdx
callq 0x2d20
movq %rax, -0x228(%rbp)
jmp 0x272d
movq -0x228(%rbp), %rsi
movq 0x8895(%rip), %rdi      # 0xafd0
callq 0x21b0
movq %rax, -0x230(%rbp)
jmp 0x2749
movq -0x230(%rbp), %rdi
movq 0x8861(%rip), %rsi      # 0xafb8
callq 0x2110
jmp 0x275e
leaq -0x1a0(%rbp), %rdi
callq 0x2f00
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x2df0
movq -0x238(%rbp), %rdi
movq %rax, -0x1a8(%rbp)
callq 0x2e20
movq %rax, -0x1b0(%rbp)
movl $0x6, -0x1c4(%rbp)
leaq -0x4a8(%rip), %rsi      # 0x2300
movq 0x8819(%rip), %rdx      # 0xafc8
leaq -0x1c0(%rbp), %rdi
leaq -0x1c4(%rbp), %rcx
callq 0x2f80
jmp 0x27c4
movq -0x1a8(%rbp), %rdi
movq -0x1b0(%rbp), %rsi
leaq -0x1c0(%rbp), %rdx
callq 0x2f20
movq %rax, -0x240(%rbp)
jmp 0x27e7
movq -0x240(%rbp), %rsi
movq 0x87db(%rip), %rdi      # 0xafd0
callq 0x21b0
movq %rax, -0x248(%rbp)
jmp 0x2803
movq -0x248(%rbp), %rdi
movq 0x87a7(%rip), %rsi      # 0xafb8
callq 0x2110
jmp 0x2818
movl $0x0, -0x4(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x2f00
leaq -0x120(%rbp), %rdi
callq 0x2fd0
leaq -0x100(%rbp), %rdi
callq 0x20b0
leaq -0xe0(%rbp), %rdi
callq 0x2ff0
leaq -0x50(%rbp), %rdi
callq 0x3050
movl -0x4(%rbp), %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x28d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x2d00
jmp 0x28d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x28ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x2f00
leaq -0x140(%rbp), %rdi
callq 0x2f00
leaq -0x120(%rbp), %rdi
callq 0x2fd0
leaq -0x100(%rbp), %rdi
callq 0x20b0
leaq -0xe0(%rbp), %rdi
callq 0x2ff0
leaq -0x50(%rbp), %rdi
callq 0x3050
movq -0xa0(%rbp), %rdi
callq 0x21a0
nop