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 )
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'; | ^~~
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;
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 ***