There is no good way to do what you're trying to do with just the CPP. Especially since you are calculating GPIO number at runtime.
The only way to make sure that it's a direct substitution at compile-time is to use the indexes directly (which you're not doing)
Consider this as a solution in C:
const long GPIO_PINS[] = { 0xFFFF, 0x0001, 0x00CA, 0x00DE, 0x00AC };
static inline long getGPIOAddr(int gpioAddress) {
return GPIO_PINS[gpioAddress >> 1];
}
int main() {
const int gpioNumber = 8;
return getGPIOAddr(gpioNumber);
}
Here is the assembly output of gcc (clang gives something similar):
gcc -S -O foo.c
.file "foo.c"
.text
.globl main
.type main, @function
main:
.LFB1:
.cfi_startproc
movl $172, %eax ; NOTICE THE OPTIMIZATION HERE!
ret
.cfi_endproc
.LFE1:
.size main, .-main
.globl GPIO_PINS
.section .rodata
.align 32
.type GPIO_PINS, @object
.size GPIO_PINS, 40
GPIO_PINS:
.quad 65535
.quad 1
.quad 202
.quad 222
.quad 172
.ident "GCC: (GNU) 7.2.0"
.section .note.GNU-stack,"",@progbits
For completeness here is clang:
.file "foo.c"
.text
.globl main
.align 16, 0x90
.type main,@function
main: # @main
.cfi_startproc
# BB#0:
movl $172, %eax
ret
.Ltmp0:
.size main, .Ltmp0-main
.cfi_endproc
.type GPIO_PINS,@object # @GPIO_PINS
.section .rodata,"a",@progbits
.globl GPIO_PINS
.align 16
GPIO_PINS:
.quad 65535 # 0xffff
.quad 1 # 0x1
.quad 202 # 0xca
.quad 222 # 0xde
.quad 172 # 0xac
.size GPIO_PINS, 40
.ident "clang version 3.4.2 (tags/RELEASE_34/dot2-final)"
.section ".note.GNU-stack","",@progbits
any good modern compiler can determine that gpioNumber does not change during a particular AST and will substitute the constant properly.
So stop trying to outsmart the compiler, it is MUCH better at optimizations than you are.
If you are interested in how far back the compilers are good at this, I've loaded this code at godbolt here:
https://godbolt.org/g/EWjsHB
You can look at disassembly of the code at your heart's content. all compilers there optimize this stuff out with a -O as the option.