在 Lua 5.3 中,C API 中的表相关函数接收和返回lua_Integer
。
void lua_rawgeti (lua_State *L, int idx, lua_Integer n);
void lua_rawseti (lua_State *L, int idx, lua_Integer n);
lua_Integer luaL_len (lua_State *L, int index);
但是,lua_createtable
还是收到int
。
void lua_createtable (lua_State *L, int narr, int nrec);
在下面的示例函数中,源表的长度用于创建相同大小的副本。
static int copy_sequence(lua_State *L) {
lua_Integer len, i;
luaL_checktype(L, 1, LUA_TTABLE);
len = luaL_len(L, 1);
lua_createtable(L, (int)len, 0); /* conversion warning */
for (i = 1; i <= len; i++) {
lua_rawgeti(L, 1, i);
lua_rawseti(L, -2, i);
}
return 1;
}
但是,需要强制转换才能使警告静音:
警告:从 'lua_Integer' 转换为 'int' 可能会改变其值 [-Wconversion]
在 Lua 邮件列表中搜索,我发现以下关于 Lua 5.2 的线程(我假设也适用于早期版本):
引用:Roberto Ierusalimschy(2012 年 8 月 7 日)
表的大小已经限制为 2147483647 个元素。Lua 内部使用 'int' 来索引它的所有数组(字符串/字节数组除外)。在任何地方都使用无符号值(例如 size_t)是一种痛苦;ptrdiff_t 根本没有任何保证。
long long
使用for的 Lua 5.3 仍然是这种情况lua_Integer
吗?在 Lua 5.3 中,上面示例中使用的转换为int
from是否安全?lua_Integer