gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:13:24 26 4
gpt4 key购买 nike

我在一个名为 Selene 的项目中看到过这个(一个 C++11 Lua 包装器)我在想它的作用是什么?

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

它是一个类(选择器)的私有(private)成员。

周边代码:

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功能(and more)。在这种情况下,它创建了一个名为 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 .

关于C++ 这行代码是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21644207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com