简体   繁体   English

标准C ++ 14中零大小数组的解决方法?

[英]Workaround for zero-sized arrays in standard C++14?

I am developing a binding toolkit for Google's V8 ECMAScript engine. 我正在为Google的V8 ECMAScript引擎开发绑定工具包。 Consider this templated function: 考虑以下模板函数:

    template<class... Types> void call_v8_function(v8::Local<v8::Function> function,
                                                   Types... args) {
        ...significant amount of context set-up here...

        v8::Local<v8::Value> call_args[] = { V8Utils::to_js_value(isolate, args)... };

        function->Call(this->context, sizeof...(args), call_args);
    }

It takes a variable number of arguments and maps them to an array of v8::Value objects using a helper function. 它使用可变数量的参数,并使用辅助函数将它们映射到v8 :: Value对象的数组。 (the arguments can have arbitrary types, as long as they're recognized by to_js_value ) This works wonderfully when I compile using GCC or Clang. (参数可以具有任意类型,只要它们可以被to_js_value识别to_js_value )当我使用GCC或Clang进行编译时,这非常to_js_value

In Visual Studio, however, it completely breaks down. 但是,在Visual Studio中,它完全崩溃了。 When this templated function is called without any additional arguments, an array of size 0 is being declared. 在不带任何其他参数的情况下调用此模板化函数时,将声明大小为0的数组。 While accepted by GCC and Clang, the standard doesn't permit this and Visual Studio rightfully spits out error C2466: cannot allocate an array of constant size 0 . 当被GCC和Clang接受时,该标准不允许这样做,并且Visual Studio正确地吐出error C2466: cannot allocate an array of constant size 0

Because I find this approach to be very convenient, I'm looking for a way to make it work, other than duplicating the function for the no-arguments case, as that would lead to a significant code duplication. 因为我发现这种方法非常方便,所以我正在寻找一种使之起作用的方法,而不是针对无参数情况复制功能,因为这将导致大量的代码重复。

Instead of c-style array, you could use a std::array which already has a specialization for zero-sized arrays. 可以使用std::array代替c样式的数组,该std::array已经对零大小的数组进行了专门化处理。 Furthermore, for your function call the data() member is also well-behaved for empty arrays. 此外,对于您的函数调用, data()成员对于空数组也表现良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM