Undefined reference to static constexpr, if passed as function parameter pack with O0 (works with higher optimization levels) [duplicate]

The following code snippet can only be linked if optimization level was higher than O0:

    #include <cstdio>
    #include <utility>
    void vf(std::size_t n, ...) {printf("%zun", n);}
    template<typename ...ARGS> void vt(ARGS&&... args) {vf( sizeof...(ARGS), std::forward<ARGS>(args)... );}
    struct X {static constexpr int a = 123; X() {vt(a);}};
    int main() {X();}

You can run it here: http://cpp.sh/3dv7p

Configuring C++11/14 with O0 will fail with the following linker error:

/tmp/cc1xC4HI.o: In function `X::X()':
:(.text._ZN1XC2Ev[_ZN1XC5Ev]+0xd): undefined reference to `X::a'
collect2: error: ld returned 1 exit status

Choosing O1, O2 or O3 will link successfully and the program execution returns the expected output.

Is this a compiler issue?

Source: Windows Questions C++

LEAVE A COMMENT