C ++ What does this line of code mean?

I saw this in a project called Selene (C ++ 11 Lua shell) and I wandered what it does

using Fun = std::function<void()>;
using PFun = std::function<void(Fun)>;

This is a private member of the class (Selector).

Ambient code:

namespace sel {
class State;
class Selector {
private:
    friend class State;
    State &_state;
    using Fun = std::function<void()>;
    using PFun = std::function<void(Fun)>;

    // Traverses the structure up to this element
    Fun _traverse;
    // Pushes this element to the stack
    Fun _get;
    // Sets this element from a function that pushes a value to the
    // stack.
    PFun _put;

    // Functor is stored when the () operator is invoked. The argument
    // is used to indicate how many return values are expected
    using Functor = std::function<void(int)>;
    mutable std::unique_ptr<Functor> _functor;

    Selector(State &s, Fun traverse, Fun get, PFun put)
        : _state(s), _traverse(traverse),
          _get(get), _put(put), _functor{nullptr} {}

    Selector(State &s, const char *name);
+3
source share
1 answer

This is C ++ 11 syntax that spans functionality typedef( and much more ). In this case, it creates an alias Funthat is the same type as std::function<void()>:

using Fun = std::function<void()>; // same as typedef std::function<void()> Fun

This means you can do this:

void foo() 
{
  std::cout << "foo\n";
}

Fun f = foo; // instead of std::function<void()> f = foo;
f();

Similarly for PFun.

+7
source

All Articles