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

            Bug ID: 96630
           Summary: dangling reference accepted in constexpr evaluation
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gccbugbjorn at fahller dot se
  Target Milestone: ---

Created attachment 49065
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49065&action=edit
Example code

This program uses a reference to an out-of-scope local variable in constexpr
context.

struct ii
{
    int v;
    constexpr const int& get() const { return v;}
    constexpr ii& inc() { ++v; return *this;}
};

struct wrap
{
    ii i;
    constexpr const int& getnext() const { auto copy = i; return
copy.inc().get();}
};

template <int> struct S {};
S<wrap{{3}}.getnext()> s;

It is, however, accepted and compiles. If used in runtime evaluation, the
undefined behaviour causes wildly different results depending on optimization
level.

bf :-) confuciusornis /tmp> g++-10 --version
g++-10 (Ubuntu 10-20200411-0ubuntu1) 10.0.1 20200411 (experimental) [master
revision bb87d5cc77d:75961caccb7:f883c46b4877f637e0fa5025b4d6b5c9040ec566]
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

bf :-) confuciusornis /tmp> g++-10 -std=c++20 -c -Wall -Wextra -pedantic t.cpp
bf :-) confuciusornis /tmp> 


Godbolt link: https://godbolt.org/z/8q91dv

Reply via email to