[Bug c++/95789] New: Const method is allowed to return non-const reference on template class
ivan at chebykin dot org
gcc-bugzilla@gcc.gnu.org
Sat Jun 20 18:23:41 GMT 2020
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95789
Bug ID: 95789
Summary: Const method is allowed to return non-const reference
on template class
Product: gcc
Version: 10.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: ivan at chebykin dot org
Target Milestone: ---
Created attachment 48762
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48762&action=edit
Preprocessed file without iostream
Hello, I've recently updated from gcc 9.3.0 to 10.1.0 and found some unexpected
behaviour when trying to compile this code:
#include <iostream>
struct B {
int n;
};
template <typename T>
struct A {
B& get() const { return f; }
B f;
};
int main() {
A<int> a;
a.f = {};
a.get().n = 10;
std::cout << a.f.n << std::endl;
return 0;
}
As I understand this code shouldn't compile with error
binding reference of type ‘B&’ to ‘const B’ discards qualifiers
but this code compiles successfully. What is more interesting is that this code
will stop compiling if I remove template or if I use POD type like int.
Neither gcc 9.3.0 compiles this code nor clang does.
GCC info:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/10.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id
--enable-lto --enable-multilib --enable-plugin --enable-shared
--enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-libunwind-exceptions --disable-werror
gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.1.0 (GCC)
Compiling with -Wall -Wextra yields no output warnings or errors.
I've attached preprocessed file without iostream.
More information about the Gcc-bugs
mailing list