Bug 96716 - GCC reports "object is private" when it's accessed through proper accessor
Summary: GCC reports "object is private" when it's accessed through proper accessor
Status: RESOLVED DUPLICATE of bug 97740
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2020-08-19 20:36 UTC by Vorfeed Canal
Modified: 2025-09-03 14:12 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-08-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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 ***