C++ Logo


Advanced search

hidden friends and member functions

From: Federico Kircheis <federico_at_[hidden]>
Date: Mon, 10 Oct 2022 22:07:35 +0200
If this is the wrong mailing list, sorry, maybe you can redirect me to
the correct one.

Consider following program.

namespace ns{
     struct foo{
         friend void bar(const foo&){}
void bar2(ns::foo& f){
auto lambda = [](ns::foo& f_){bar(f_);};
struct foo2{
     ns::foo mf;
     void bar(){
         bar(this->mf); // (1)
         auto& f2 = mf; [&f2]{bar(f2);}(); // (2)
         [](ns::foo& f_){bar(f_);}(mf); // (3)
         bar2(mf); // (4)
         lambda(mf); // 5
         ns::bar(mf); // (6)
         ::bar(mf); // (7)
1) does not compile, tries to call this->bar, no great, but OK
2,3) Still tries to call this->bar, even if this has not been captured
4,5) Works
6,7) do not compile, as expected
(tested with GCC, clang and MSVC)
I understand why 1,4,5,6,7 behave as they do, but fail to see why 2 and 
3 should not compile and call the hidden friend function.
4 and 5 are suboptimal workarounds because they need a new function, if 
the code is templated it needs to go in the header file, and thus leak 
an implementation detail, if 2 or 3 would work it would be possible to 
keep the workaround local.
Or is there another way to tell the compiler not trying to call 
this->bar, without resorting to 6,7 as they do not work in this case?
I tried finding the relevant part in the standard why 2 and 3 do not 
work, but failed to do so, can anyone pinpoint me in the right direction?

Received on 2022-10-10 20:07:41