How to use "use" for a function?

How to use "use" for a function? for instance

class A;
void f(int);

struct B
{
   using BA = A;
   using Bf = f; ???
};
+6
source share
4 answers

You can do

struct B
{
   using BA = A;
   constexpr static auto Bf = f;
}

This way, you don’t have to worry about specifying a type that can be annoying.

, . , , . , , , Bf f, . constexpr .

+11

f - , -:

void (*Bf)(int) = f;

:

void Bf(int i) { f(i); }

, , B.


f , , :

template <class... Args>
auto Bf(Args&&... args)
    -> decltype(::f(std::forward<Args>(args)...))
    noexcept(noexcept(::f(std::forward<Args>(args...))))
{
    return ::f(std::forward<Args>(args)...);
}

... .

+2

, , f.

++ 14 :

struct B {
  using BA = A;
  static constexpr auto BF = [](auto&&... args) {
    return f(std::forward<decltype(args)>(args)...);
  };
};

, - std::forward . std::forward, ( , , f ):

struct B {
  using BA = A;
  static constexpr auto BF = [](auto... args) { return f(args...); };
};
+2

, A - , f - . using Bf f, :

using Bf = decltype(f);
0

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


All Articles