C ++这行代码是什么意思?

时间:2014-02-08 09:18:21

标签: c++ c++11 using member

我在一个名为Selene的项目(C ++ 11 Lua包装器)中看到过这种情况,我正在徘徊它的作用吗?

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

它是类(Selector)的私有成员。

周围代码:

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);

1 个答案:

答案 0 :(得分:7)

这是一种涵盖typedef功能(and more)的C ++ 11语法。 在这种情况下,它会生成一个名为Fun的别名,它与std::function<void()>的类型相同:

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

这意味着你可以这样做:

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

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

同样适用于PFun