There is little in the way of optimization in even the newest C++ standard today (2021), e.g. copy elision and return value optimization.
This leaves 'compilers' to apply any platform-specific optimizations.
The function in the question produces no effect and is thus most likely optimized away completely.
But to address (what I am assuming is) the 'underlying' question, a typical compiler will be able to infer that the same condition is applied even to a nested loop, e.g.
int foo(vector<vector<int>> a, vector<vector<int>> b, bool flag) {
int value = 0;
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) {
if (flag)
value += a[i][j] + b[i][j];
else
value += a[i][j] - b[i][j];
}
}
return value;
}
Assembly code below says 'yes, it is optimized' (generated by Clang Intel x86 64-bit compiler):
- notice third argument
foo
found in register dl
(an 8-bit version of 64-bit register RDX
) is tested before the two loops start
- both loops have been duplicated based on the 'foo' condition:
LBB1_2
and LBB1_6
This (redacted) assembly code as generated by running: g++ -std=c++17 -O3 -c -S code.cpp
:
__Z3fooNSt3__16vectorINS0_IiNS_9allocatorIiEEEENS1_IS3_EEEES5_b: ## @_Z3fooNSt3__16vectorINS0_IiNS_9allocatorIiEEEENS1_IS3_EEEES5_b
.cfi_startproc
## %bb.0:
pushq %rbp
...
xorl %eax, %eax # <======== int value = 0;
testb %dl, %dl # <======== if (flag)
jne LBB1_6
jmp LBB1_2
.p2align 4, 0x90
LBB1_7: ## in Loop: Header=BB1_6 Depth=1
incq %r10
cmpq %r10, %r8
jbe LBB1_26
LBB1_6: ## =>This Loop Header: Depth=1
## Child Loop BB1_13 Depth 2
## Child Loop BB1_17 Depth 2
leaq (%r10,%r10,2), %rcx
movq (%r9,%rcx,8), %rdi
movq 8(%r9,%rcx,8), %r11
subq %rdi, %r11
...
LBB1_2: ## =>This Loop Header: Depth=1
## Child Loop BB1_21 Depth 2
## Child Loop BB1_5 Depth 2
leaq (%r10,%r10,2), %rcx
movq (%r9,%rcx,8), %rdi
movq 8(%r9,%rcx,8), %r11
subq %rdi, %r11