Open bluetarpmedia opened 4 weeks ago
Thanks! More generally, Cpp2 doesn't yet support "magic statics" / static
local variables, whether constexpr
or not. I think your request is to support a "magic statics" equivalent.
I was dragging my feet on supporting that because call_once
is still available for single-lazy-initialization semantics, so I was waiting to see if there was demand for the feature. However, call_once
doesn't let you do the initialization of the constexpr
value you want.
So it seems that a "static" or "once" or similar qualifier or similar is wanted, that lowers to magic statics...
Describe the bug I can't find a Cpp2 syntax to emit a
static constexpr
local variable.static constexpr
variables are useful (particularly in debug/non-optimised builds) for look-up tables in a function where you want the data initialised once and also prefer to keep the variable close to its use in the function, rather than declaring it globally.See:
constexpr
(And Use This [static constexpr] Instead!)To Reproduce Run cppfront on this code:
It lowers to:
which produces a C++ compiler error:
Repro on Godbolt
Why
static constexpr
Here's a C++ example demonstrating why
static constexpr
is useful:Side-by-side repro on Godbolt
With
static constexpr
MSVC Release `/O2`
```assembly main PROC ; COMDAT mov eax, 33 ; 00000021H ret 0 main ENDP ```MSVC Debug `/Od`
```assembly std::arrayWithout
static constexpr
MSVC Release `/O2`
```assembly ints$ = 32 __$ArrayPad$ = 48 main PROC ; COMDAT $LN41: sub rsp, 72 ; 00000048H mov rax, QWORD PTR __security_cookie xor rax, rsp mov QWORD PTR __$ArrayPad$[rsp], rax mov r8d, 33 ; 00000021H mov DWORD PTR ints$[rsp], 11 lea rdx, QWORD PTR ints$[rsp+12] mov DWORD PTR ints$[rsp+4], 22 lea rcx, QWORD PTR ints$[rsp] mov DWORD PTR ints$[rsp+8], 33 ; 00000021H call __std_find_trivial_4 mov eax, DWORD PTR [rax] mov rcx, QWORD PTR __$ArrayPad$[rsp] xor rcx, rsp call __security_check_cookie add rsp, 72 ; 00000048H ret 0 main ENDP ```MSVC Debug `/Od`
```assembly voltbl SEGMENT DDSymXIndex: FLAT:main voltbl ENDS $T1 = 32 $T2 = 40 $T3 = 48 it$ = 56 $T4 = 64 $T5 = 72 ints$ = 80 __$ArrayPad$ = 96 main PROC $LN3: sub rsp, 120 ; 00000078H mov rax, QWORD PTR __security_cookie xor rax, rsp mov QWORD PTR __$ArrayPad$[rsp], rax mov DWORD PTR ints$[rsp], 11 mov DWORD PTR ints$[rsp+4], 22 mov DWORD PTR ints$[rsp+8], 33 ; 00000021H mov DWORD PTR $T1[rsp], 33 ; 00000021H lea rdx, QWORD PTR $T4[rsp] lea rcx, QWORD PTR ints$[rsp] call std::_Array_const_iterator