Bug 96716

Summary: GCC reports "object is private" when it's accessed through proper accessor
Product: gcc Reporter: Vorfeed Canal <vorfeed.canal>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: ppalka, StevenSun2021, ts826848, webrown.cpp
Priority: P3 Keywords: rejects-valid
Version: 10.2.1   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109576
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121707
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2020-08-20 00:00:00
Bug Depends on:    
Bug Blocks: 55004    

Description Vorfeed Canal 2020-08-19 20:36:21 UTC
The following program doesn't work ( https://godbolt.org/z/87cr44 ):

#include <array>
#include <string_view>

class Foo {
 public:
  auto constexpr  getfoo() const -> std::string_view {
    return {foo.data(), 2};
  }
 private:
  const std::array<char, 3> foo = {'a', 'b', '\0'};
};

inline constexpr Foo foo;

template <typename>
class Bar {
 public:
  static constexpr auto bar = foo.getfoo();
};

auto& baz = Bar<int>::bar;

Error message:
<source>: In instantiation of 'constexpr const std::basic_string_view<char> Bar<int>::bar':

<source>:21:23:   required from here

<source>:18:25: error: 'const std::array<char, 3> Foo::foo' is private within this context

   18 |   static constexpr auto bar = foo.getfoo();

      |                         ^~~

<source>:10:29: note: declared private here

   10 |   const std::array<char, 3> foo = {'a', 'b', '\0'};

      |                             ^~~

Compiler returned: 1

All other compilers work fine ( https://godbolt.org/z/WvfrPf )
Comment 1 Jonathan Wakely 2020-08-20 08:06:01 UTC
Reduced:

struct string_view
{
  constexpr string_view(const char* p) : p(p) { }
  const char* p;
};

class Foo {
 public:
  constexpr string_view getfoo() const {
    return {&foo};
  }
 private:
  const char foo = 'c';
};

inline constexpr Foo foo;

template <typename>
class Bar {
 public:
  static constexpr auto bar = foo.getfoo();
};

auto& baz = Bar<int>::bar;


pr96716.C: In instantiation of 'constexpr const string_view Bar<int>::bar':
pr96716.C:24:23:   required from here
pr96716.C:21:25: error: 'const char Foo::foo' is private within this context
   21 |   static constexpr auto bar = foo.getfoo();
      |                         ^~~
pr96716.C:13:14: note: declared private here
   13 |   const char foo = 'c';
      |              ^~~
Comment 2 Jonathan Wakely 2020-08-20 08:09:06 UTC
Slightly further reduced:

struct string_view
{
  constexpr string_view(const char* p) : p(p) { }
  const char* p;
};

struct Foo {
  constexpr string_view getfoo() const {
    return {&foo};
  }
private:
  const char foo = 'c';
};

inline constexpr Foo foo;

template <typename>
struct Bar {
  static constexpr auto bar = foo.getfoo();
};

auto& baz = Bar<int>::bar;
Comment 3 Patrick Palka 2025-09-03 14:12:45 UTC
let's call this a dup of PR97740 (which is marked as a regression and has been recently fixed on trunk)

*** This bug has been marked as a duplicate of bug 97740 ***