This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/83441] New: Null this captured in lambda in class variable initializer for templated classes


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

            Bug ID: 83441
           Summary: Null this captured in lambda in class variable
                    initializer for templated classes
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alextenbrink at gmail dot com
  Target Milestone: ---

Created attachment 42896
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=42896&action=edit
Preprocessed compilation result on example

The following code prints '0', signifying that the capture 'this' pointer is
null:

#include<iostream>
#include<functional>

template<bool Dummy>
struct X {
   std::function<void()> f{[this]{ std::cout << (void*)this << "\n"; }};
};

int main() {
    X<true> x;
    x.f();
    return 0;
}

Removing the template parameter of the class, or moving the initializer to a
constructor makes the program print a nonzero value, for example:

#include<iostream>
#include<functional>

template<bool Dummy>
struct X {
   X() : f{[this]{ std::cout << (void*) this << "\n"; }}{}
   std::function<void()> f;
};

int main() {
    X<true> x;
    x.f();
    return 0;
}

My expected behaviour is that the 'this' pointer is not null. It looks like
this problem repos in previous versions of GCC - if I try this code in an
online compiler, I even get internal compiler errors on some sites.

Version and command information (I'm using a locally compiled gcc 7.2):

/usr/local/bin/g++ -v -save-temps -Wall -Wextra -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -fsanitize=undefined bug.cpp && ./a.out

Using built-in specs.
COLLECT_GCC=/usr/local/bin/g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-7.2.0/configure --enable-languages=c++
--disable-multilib
Thread model: posix
gcc version 7.2.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-fno-strict-aliasing'
'-fwrapv' '-fno-aggressive-loop-optimizations' '-fsanitize=undefined'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.2.0/cc1plus -E -quiet -v
-imultiarch x86_64-linux-gnu -D_GNU_SOURCE bug.cpp -mtune=generic -march=x86-64
-Wall -Wextra -fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations
-fsanitize=undefined -fpch-preprocess -o bug.ii
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-fno-strict-aliasing'
'-fwrapv' '-fno-aggressive-loop-optimizations' '-fsanitize=undefined'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.2.0/cc1plus -fpreprocessed bug.ii
-quiet -dumpbase bug.cpp -mtune=generic -march=x86-64 -auxbase bug -Wall
-Wextra -version -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -fsanitize=undefined -o bug.s
GNU C++14 (GCC) version 7.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 7.2.0, GMP version 6.1.0, MPFR version 3.1.4,
MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++14 (GCC) version 7.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 7.2.0, GMP version 6.1.0, MPFR version 3.1.4,
MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: c1b49aafb451e1f859453c52dc4e5f71
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-fno-strict-aliasing'
'-fwrapv' '-fno-aggressive-loop-optimizations' '-fsanitize=undefined'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v --64 -o bug.o bug.s
GNU assembler version 2.26.1 (x86_64-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.26.1
COMPILER_PATH=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.2.0/:/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.2.0/:/usr/local/libexec/gcc/x86_64-pc-linux-gnu/:/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.2.0/:/usr/local/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.2.0/:/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.2.0/../../../../lib64/:/lib/x86_64-linux-gnu/:/lib/../lib64/:/usr/lib/x86_64-linux-gnu/:/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-fno-strict-aliasing'
'-fwrapv' '-fno-aggressive-loop-optimizations' '-fsanitize=undefined'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.2.0/collect2 -plugin
/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.2.0/liblto_plugin.so
-plugin-opt=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.2.0/lto-wrapper
-plugin-opt=-fresolution=bug.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc
--eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2
/usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o
/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.2.0/crtbegin.o
-L/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.2.0
-L/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.2.0/../../../../lib64
-L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu
-L/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.2.0/../../.. bug.o -lstdc++ -lm
-lubsan -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.2.0/crtend.o
/usr/lib/x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-fno-strict-aliasing'
'-fwrapv' '-fno-aggressive-loop-optimizations' '-fsanitize=undefined'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
0

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]