https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69078

            Bug ID: 69078
           Summary: [C++14] function local static not initialized when
                    only used in a generic/variadic lambda
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mar-bwTmhx at uchuujin dot de
  Target Milestone: ---

The following reduced testcase shows that g++ doesn't initialize the local
static "instance". The original testcase is from a embedded arm usecase with
gcc 5.1.2 but this testcase is for a standard environment. As i don't have a
recent enough gcc for desktop i tested via
http://melpon.org/wandbox/permlink/gl8MdzUusCZtO7Ru with gcc version set to 6.0
HEAD

compilation: g++ prog.cc -Wall -Wextra -O2 -march=native -std=gnu++1z

Of interest might be that g++ also emits a wrong warning:

prog.cc: In function 'int main()':
prog.cc:20:18: warning: variable 'instance' set but not used
[-Wunused-but-set-variable]
     static Class instance = { probe };
                  ^~~~~~~~
This program outputs:
0x600ba8 data: (nil)
While expected output would with the '(nil)' replaced by a address of the probe
function.

When enabling the commented use of 'instance' in the normal function body (1)
of main the output is an even stranger:

0x600bd8 data: 0x400710
0x600bc8 data: (nil)

Only when rewriting the code to use an non generic lambda the bug disappears.
(http://melpon.org/wandbox/permlink/MUbb2tHz9EJCOF44) (i.e. funUser([](int p) {
user(instance, p); });


#include <stdio.h>

struct Class {
    Class(void (*_param)()) : data(_param) {}
    void (*data)();
};

void funUser(void (*test)(int)) {
    test(60);
}

void user(Class& c, int i) {
    (void)i;
    printf("%p data: %p\n", &c, c.data);
}
void probe() {}


int main() {
    static Class instance = { probe };
    //user(instance, 0); // (1)

    funUser([](auto... p) {
        user(instance, p...);
    });
}

Reply via email to