Question

I've seen this in a project called Selene (a C++11 Lua wrapper) and I was wandering what it does?

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

It is a private member of a class (Selector).

Surrounding 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);
Was it helpful?

Solution

It is a C++11 syntax which covers typedef functionality (and more). In this case, it makes an alias called Fun, which is the same type as an 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top