C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Translation-unit-local functions that access private class fields

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 29 Apr 2026 09:54:53 -0700
On Wednesday, 29 April 2026 04:46:17 Pacific Daylight Time André Offringa via
Std-Proposals wrote:
> class Foo {
> public:
> Foo() {
> F(5); // Calls double overload
> }
> private:
> void F(double) { }
> };
>
> private void Foo::F(int) { }

Please note that private members still participate in overload resolution for
non-friends. It's only after the resolution has resolved to a specific function
that access verification happens.

Take this modification from yours:
class Foo {
  public:
   void F(double) { }
  private:
#ifdef PRIV
   void F(int);
#endif
};

void f(Foo &f)
{
    f.F(1);
}
https://diff.godbolt.org/z/6a9469a6h

The free f() function is not a friend, but would have called the private if it
were defined, causing a compilation failure.
-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center - Platform & Sys. Eng.

Received on 2026-04-29 16:54:56