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

            Bug ID: 125384
           Summary: [c++] this in lambda capture in a member function of a
                    template class C resolves as C*&, not C*
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: topin89 at mail dot ru
  Target Milestone: ---

Created attachment 64499
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64499&action=edit
demo code from above with some working variations

Here’s code that works on gcc 15 but not on gcc 16:

template<typename C>
struct At{
    void b();
};

template<typename C>
void At<C>::b(){
    if constexpr(std::is_same_v<decltype(this), At*>){
        std::cout << "decltype(this) == At* \n";
    } else if constexpr(std::is_same_v<decltype(this), At*&>){
        std::cout << "decltype(this) == At*& \n";
    }

    [this](){
        if constexpr(std::is_same_v<decltype(this), At*>){
            std::cout << "decltype(this) == At* \n";
        } else if constexpr(std::is_same_v<decltype(this), At*&>){
            std::cout << "decltype(this) == At*& \n";
        }
        static_cast<void>(this);
    }();
}

Output should be:

decltype(this) == At*
decltype(this) == At*


But it’s

decltype(this) == At*
decltype(this) == At*&

Output should be:

decltype(this) == At*
decltype(this) == At*


But it’s


decltype(this) == At*
decltype(this) == At*&


If struct is not templated, everything as expected. If you remove
static_cast<void>(this); and make lambda without this captured, everything
works as expected.

attached thisbug.cpp with buggy and correct behavior

gcc version and conf flags is:

LC_ALL=C /opt/gcc16/bin/g++ -v
Using built-in specs.
COLLECT_GCC=/opt/gcc16/bin/g++
COLLECT_LTO_WRAPPER=/opt/gcc16/libexec/gcc/x86_64-pc-linux-gnu/16.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /home/user/gcc16/configure CC=/usr/local/bin/gcc
CXX=/usr/local/bin/g++ --prefix=/opt/gcc16 --enable-languages=c,c++
--disable-multilib --enable-checking=release
--with-build-config='bootstrap-lto-lean bootstrap-O3 bootstrap-native'
--with-pkgversion=gcc-16
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.1.0 (gcc-16)

cmdline:

LC_ALL=C /opt/gcc16/bin/g++ thisbug.cpp -o thisbug && ./thisbug

Compiler Explorer link: https://godbolt.org/z/j77xsx8Kv

Reply via email to