簡體   English   中英

C ++這行代碼是什么意思?

[英]C++ What does this line of code mean?

我在一個名為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);

它是一種C ++ 11語法,涵蓋了typedef功能( 以及更多 )。 在這種情況下,它創建一個名為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM