草庐IT

lua_call

全部标签

c++ - 使用 C API 的有序 lua 表循环

考虑以下lua表:foo={bar1={a=1,b=2,c="hello"},bar2={a=5,b=2,c="bbq"},bar3={a=76,b=13,c="pwnd"}}我正在尝试使用luaCAPI迭代此表以检索键名称bar1、bar2和bar3。我使用lua_next(L,-2)函数按照许多人的建议进行迭代,但问题是它以随机顺序返回元素。每次运行时顺序都会发生变化。我使用以下代码:for(lua_pushnil(L);lua_next(L,-2)!=0;lua_pop(L,1)){printf("%s\n",lua_tostring(L,-2));}大多数时候,输出是无序的,比

c++ - std::call_once 是免费的吗?

我想知道std::call_once锁是否空闲。There是使用互斥锁的call_once实现。但是我们为什么要使用互斥体呢?我尝试使用atomic_bool和CAS操作编写简单的实现。代码线程安全吗?#include#include#include#includeusingnamespacestd;usingmy_once_flag=atomic;voidmy_call_once(my_once_flag&flag,std::functionfoo){boolexpected=false;boolres=flag.compare_exchange_strong(expected,tr

c++ - 函数需要抽象类型 A 的 C++ 对象。如何将 A 子类的 Lua 对象传递给它?

我想将派生自纯虚拟C++类的类的Lua对象传递给需要纯虚拟类的C++对象的函数。我该怎么做?我是lua和luabind的新手,请耐心等待。在C++中:structA{virtualvoidfoo()=0;};voiddo_something(A*a){a->foo();}在Lua中:class'MyA'(A)....functionMyA:foo()print('hi')end再次在C++中:...//somehowcreateaninstanceofMyAclassandnamedmyA//How?//Maybetheresultofacallto"MyA()"?do_somethi

c++ - 如何从 C++ 读取 Lua 表返回值

我有一个返回表(包含字符串集)的Lua函数该函数使用此代码运行良好:lua_pushstring(lua,"funcname");lua_gettable(lua,LUA_GLOBALSINDEX);lua_pushstring(lua,"someparam");lua_pcall(lua,1,1,0);该函数返回一个表。我如何从我的C++代码中读取它的内容? 最佳答案 如果你问如何遍历结果表,你需要lua_next(该链接还包含一个示例)。正如egarcia所说,如果lua_pcall返回0,则可以在堆栈顶部找到函数返回的表。

c++ - std::call_once 和内存重新排序

给定来自here的代码:classlazy_init{mutablestd::once_flagflag;mutablestd::unique_ptrdata;voiddo_init()const{data.reset(newexpensive_data);}public:expensive_dataconst&get_data()const{std::call_once(flag,&lazy_init::do_init,this);return*data;}};我在其他地方也看到了相同模式的一些变体。所以我的问题是:为什么这段代码被认为是保存的?以及为什么编译器不能在调用std::c

c++ - std::runtime_error 子类的 "call to deleted constructor of"编译器错误

我从std::runtime_error派生了一个异常类,以便添加对异常流的支持。我收到一个奇怪的编译器错误输出,我不确定如何解决?clang++-std=c++11-stdlib=libc++-g-Wall-I../-I/usr/local/includeMain.cpp-cMain.cpp:43:19:error:calltodeletedconstructorof'EarthException'throwEarthException(__FILE__,__LINE__)^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~../EarthExce

c++ - 如何替换 Lua 默认错误打印?

我正在将Lua作为脚本语言实现到Windows应用程序中。由于应用程序的结构,打印输出未使用流式io,例如stdout和stderror。我已经设法覆盖Luaprint以适应我的结构...lua_register(L,"print",cs_print);...但是如何在不使用流的情况下覆盖所有错误和调试打印输出?我需要在一个函数中处理它(类似于print)。 最佳答案 Lua写入stderr的唯一地方是在luaL_newstate的panic函数中。安装。如果您将Lua嵌入到您的应用程序中,请确保您从protected调用中启动Lu

c++ - 最烦人的解析 : why doesn't `g( ( f() ) );` call `f` 's default constructor and pass the result to `g` 's ctor that takes a `f` ?

这不是Mostvexingparse:whydoesn'tAa(());work?的拷贝,它基于Aa());形式的解析,其OP认为可以使用额外的集合默认构造一个A对象括号。相比之下,我的问题是关于2个类,f和g,其中f具有默认构造函数,而g的构造函数采用f。我想用一个临时的f参数调用g的构造函数,而不使用统一的初始化语法。g的构造函数中有一个std::cout语句,因此缺少输出表示函数声明而不是g对象实例化。我在注释中用3个数字注释了示例代码。#1和#2编译时#3被注释掉,反之亦然:#includestructf{};structg{g(f){std::cout#1:我认为#1声明了一

c++ - 错误 : no matching function for call to ‘to_string(std::basic_string<char>&)’

即使在模板中我可以有任何类型,函数to_string对基本字符串不起作用:例如:std::stringstr("mystring");my_class(str);用这个仿函数定义:templatevoidoperator()(valuetypevalue){...private_string_field=std::to_string(value);不起作用。这是错误:error:nomatchingfunctionforcallto‘to_string(std::basic_string&)’避免它的最佳方法是什么。事先,我要求避免仅仅因为一些常见的关键字就链接到不相关的问题。

c++ - "no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::string&)’“

我正在尝试使用std::async创建线程,但我不断收到错误“没有匹配函数调用‘async(std::launch,,std::string&)’”在行上ConnectFuture=std::async(std::launch::async,Connect_T,ip);这是产生这种行为的代码:#includeclasslibWrapper{public:voidConnect(std::stringip);voidConnect_T(std::stringip);private:std::futureConnectFuture;};voidlibWrapper::Connect(std