public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95789] New: Const method is allowed to return non-const reference on template class
@ 2020-06-20 18:23 ivan at chebykin dot org
  2020-06-21  9:03 ` [Bug c++/95789] [10/11 Regression] " redi at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: ivan at chebykin dot org @ 2020-06-20 18:23 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-07-14 23:26 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-20 18:23 [Bug c++/95789] New: Const method is allowed to return non-const reference on template class ivan at chebykin dot org
2020-06-21  9:03 ` [Bug c++/95789] [10/11 Regression] " redi at gcc dot gnu.org
2020-06-22 19:31 ` mpolacek at gcc dot gnu.org
2020-07-07 16:10 ` mpolacek at gcc dot gnu.org
2020-07-07 23:11 ` mpolacek at gcc dot gnu.org
2020-07-07 23:11 ` mpolacek at gcc dot gnu.org
2020-07-13 17:35 ` mpolacek at gcc dot gnu.org
2020-07-13 17:36 ` mpolacek at gcc dot gnu.org
2020-07-14 20:03 ` cvs-commit at gcc dot gnu.org
2020-07-14 23:24 ` cvs-commit at gcc dot gnu.org
2020-07-14 23:26 ` mpolacek at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).