Using declarations for a specific overloaded function

This code

struct Foo{
    void f(){
        f(0);
    }
private:
    void f(int){}
};

struct Bar : private Foo{
    using Foo::f;
};

int main() {
    Bar b;
    b.f();
}

failed to compile because Foo::f(int)- private. I'm not interested Foo::f(int), I just want Foo::f()that, which publicis why I think there should be a way to do this.

There are some workarounds I can think of:

  • rename Foo::f(int)to Foo::p_f(int), but this is redundant and prohibits overload resolution forf
  • the implementation Bar::foo(){Foo::f();}becomes a large copy / paste for multiple public fs
  • inherits publicly from Foo, which invites UB, as ~Foo()not virtual(and is not supposed to)
  • making everyone f publicmakes accidental breaking too easy FooandBar

using public Foo::f;? ?

+4
2

f(int) private API, fimpl:

struct Foo{
    void f(){
        fimpl(0);
    }
private:
    void fimpl(int){}
};

, , f(int) API, , , f(int) a public .

struct Foo{
    void f(int = 0){}
};

, , ,

struct Foo{
    void f(int) {} // general interface
    void sensible_name1() { f(0); }
    void sensible_name2() { f(1); } // etc.
}
0

Bar Foo private. Foo::f() Bar. , struct Bar: public Foo.

. " " . , , :

[w] , ( , ),

-1

Source: https://habr.com/ru/post/1625098/


All Articles