Using an alias for static member functions?

Is there a way to pseudonize a static member function in C ++? I would like to be able to cover it, so that I do not need to fully qualify the name.

Essentially something like:

struct Foo {
  static void bar() {}
};

using baz = Foo::bar; //Does not compile

void test() { baz(); } //Goal is that this should compile

My first thought was to use std::bind(as in auto baz = std::bind(Foo::bar);) or function pointers (as in auto baz = Foo::bar;), but this is unsatisfactory, because for each function I want to be able to use an alias in, I need to make a separate variable only for this function or instead make an alias variable available in global / static area.

+2
source share
2 answers

using . ( , ) auto baz = &Foo::bar.

, constexpr , , .

struct Foo {
  static void bar() { std::cout << "bar\n"; }
};

constexpr auto baz = &Foo::bar; 

void test() { baz(); }

int main() 
{
    test();
}

+5

, .

void (*baz)()  = Foo::bar; //Does not compile

baz(); Foo:: bar;

0

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


All Articles