gpt4 book ai didi

c++ - 避免警告 : operation on ‘count’ may be undefined [-Wsequence-point]

转载 作者:太空狗 更新时间:2023-10-29 21:33:40 25 4
gpt4 key购买 nike

我正在使用 Node.js N-API,我正在制作一个小包装器,这将使导出 C++ 函数变得容易。

template<class T, class... Targs> napi_value Api::create(const char* name, T (* const cb)(Targs...))
{
// creates JavaScript function that will call cbProxy<> when called
return create(name, cbProxy<T, Targs...>, cb);
}

template<class T, class... Targs> napi_value Api::cbProxy(const napi_env env, const napi_callback_info info)
{
// number of arguments
size_t count = sizeof...(Targs);
ApiValue args[count];
T (* cb)(Targs...);
// retrieve arguments and callback
if (!Api::getParams(env, info, args, count, &cb))
return nullptr;

T ret = cb(Api::getValue<Targs>(&args[--count])...);
return Api(env).create(ret);
}

template<> bool Api::getValue(ApiValue* value)
{
return value->toBool();
}

template<> double Api::getValue(ApiValue* value)
{
return value->toDouble();
}

template<> int32_t Api::getValue(ApiValue* value)
{
return value->toInt32();
}

想法是调用api.create("TestFunction", testFn);它将返回一个 JS 函数。
当它被调用时,它会调用 Api::cbProxy<>使用 Api::getValue<T>() 将 JS 参数转换为等效的 C++ 类型并调用 testFn (cb).

让我们说 testFn具有以下签名:int testFn(bool bVal, double dVal)
线路T ret = cb(Api::getValue<Targs>(&args[--count])...);将扩展到

int ret = cb(Api::getValue<bool>(&args[--count]), 
Api::getValue<double>(&args[--count]));

100% 正常工作,但会触发编译器警告。我是 C++ 的新手,所以我正在寻找一种更好的方法来编写它。

基本上我想遍历数组args和类型参数列表 Targs一次。

澄清一下:--count在扩展函数调用中触发以下警告:
warning: operation on ‘count’ may be undefined [-Wsequence-point]

最佳答案

我想问题是在

int ret = cb(Api::getValue<bool>(&args[--count]), 
Api::getValue<double>(&args[--count]));

参数参数的评估顺序是依赖于实现未定义的行为(M.M 校正)因此,给定从2 开始的count,可以是

int ret = cb(Api::getValue<bool>(&args[1]), 
Api::getValue<double>(&args[0]));

int ret = cb(Api::getValue<bool>(&args[0]), 
Api::getValue<double>(&args[1]));

要确保 args 的第一个索引(bool 一个)是 1 而第二个( double one) 是 0,一种可能的方法是使用可变索引。

如果你可以使用 C++14,使用 cbProxyHelper() 方法,你可以尝试如下操作(注意:代码未测试)

template <typename T, typename ... Targs, std::size_t ... Is>
napi_value Api::cbProxyHelper (const napi_env env,
const napi_callback_info info,
std::index_sequence<Is...> const &)
{
// number of arguments
constexpr std::size_t count = sizeof...(Targs);
ApiValue args[count];
T (* cb)(Targs...);
// retrieve arguments and callback
if (!Api::getParams(env, info, args, count, &cb))
return nullptr;

T ret = cb(Api::getValue<Targs>(&args[count-1U-Is])...);
return Api(env).create(ret);
}

template <typename T, typename ... Targs>
napi_value Api::cbProxy (const napi_env env, const napi_callback_info info)
{ return cbProxyHelper(env, info, std::index_sequence_for<Targs...>{}); }

如果您使用的是 C++11,模拟 std::index_sequencestd::make_index_sequence 并不难。

关于c++ - 避免警告 : operation on ‘count’ may be undefined [-Wsequence-point],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50520583/

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