https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106969
Bug ID: 106969 Summary: member function constness incorrectly propagates to local class member function return type deduction Product: gcc Version: 12.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: enolan at alumni dot cmu.edu Target Milestone: --- When using decltype in the return type of a member function of a local class defined inside of a const member function, the type of 'this' seems to be deduced as a pointer to const, when it should be a pointer to non-const. Compiler explorer link: https://godbolt.org/z/9KKbrzq1e Minimal reproduction: struct Context { void action() const { struct { int wrapped; decltype( & wrapped ) get() { return &wrapped; } } t; *t.get()= 42; } }; Note that if the local class member function definition is changed to: auto get() -> decltype( & wrapped ) { return &wrapped; } Then the bug no longer appears. > the exact version of gcc gcc version 12.2.1 20220819 (Red Hat 12.2.1-2) (GCC) > the system type Fedora release 38 (Rawhide) > the options given when GCC was configured/built Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-12.2.1-20220819/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-offload-defaulted --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1 > the complete command line that triggers the bug g++ poc.cpp > the compiler output poc.cpp: In member function ‘void Context::action() const’: poc.cpp:12:17: error: assignment of read-only location ‘* t.Context::action() const::<unnamed struct>::get()’ 12 | *t.get()= 42; | ~~~~~~~~^~~~