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

            Bug ID: 67813
           Summary: [C++14] copy-initialization of object with pointer
                    member fails in constexpr function
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Casey at Carter dot net
  Target Milestone: ---

r223851 fails to compile this correct program fragment:

struct Ptr {
  int* p;

  constexpr Ptr(int* p) noexcept : p{p} {}
  constexpr int& operator*() const {
    return *p;
  }
};

constexpr int f(int& i) {
  //Ptr first{&i}; // Works.
  Ptr first = &i;  // Error
  return *first;
}

constexpr int g() {
  int i = 42;
  return f(i);
}

static_assert(g() == 42);

with error:

~/gcc6-r228351/bin/g++ -std=c++14 foo.cpp -c
foo.cpp:21:1: error: non-constant condition for static assertion
 static_assert(g() == 42);
 ^
foo.cpp:21:16:   in constexpr expansion of ‘g()’
foo.cpp:18:11:   in constexpr expansion of ‘f(i)’
foo.cpp:13:11:   in constexpr expansion of ‘first.Ptr::operator*()’
foo.cpp:21:1: error: accessing uninitialized member ‘Ptr::p’

If the local is instead direct-initialized with "Ptr first{&i};", the program
correctly compiles without diagnostics. Results are identical if Ptr's member p
is type int& (with the constructor and operator* updated accordingly).

Reply via email to