如何从C++中最好地调用具有C99风格数组函数签名的函数
How to best call functions with C99-style array function signatures from C++
我正在用C++编写一些代码,需要调用用C99编写的库。此库使用C99样式的数组声明,函数参数中包含static
关键字。即:
void my_func(int n, int my_ints[static n]);
然而,当在我的C++项目中包含这个库的头时,编译器(clang)在使用-pedantic
标志时抛出警告:
> g++ -pedantic -c my_code.cpp
In file included from my_code.cpp:
./my_c_lib.h: warning: variable length arrays are a C99 feature [-Wvla-extension]
void my_func(int n, int my_ints[static n]);
在这种情况下,调用C库的正确/最佳方式是什么?除了关闭vla-extension
警告之外,是否还有一些方法可以绕过它,不需要重写库的头或编写中间的C包装器?
最小工作示例:
extern "C" {
void my_func(int n, int my_ints[static n]);
}
int main()
{
int* some_ints = new int[10];
my_func(10, some_ints);
delete[] some_ints;
return 0;
}
事实是,C++根本没有像C99那样强大的VLA,而且很可能永远不会有;将VLA纳入该语言的进展受到了严重限制,几乎毫无用处。
也就是说,您最好的选择可能是为您实际使用的库函数编写一些包装器,这些包装器公开风格的接口
void my_func_wrap(int n, int* my_ints);
这些将在C99文件中实现,如下所示:
void my_func_wrap(int n, int* my_ints) {
my_func(n, my_ints);
}
C头和带有实现的文件都可以从库头自动生成,因为更改几乎是微不足道的。现在,您可以在没有任何类型冲突的情况下从C++代码中调用包装器。
第二种可能的方法是编写从库头中剥离所有[]
括号内容的脚本,并使用它。这将完美地工作,因为即使在C99中,声明
void my_func_wrap(int n, int my_ints[static n]);
衰变为
void my_func_wrap(int n, int* my_ints);
这就是为什么我不需要在上面的包装器函数中进行任何强制转换的原因(我知道这听起来很疯狂,但这是事实)。只是您的C++编译器不喜欢第一个语法变体。
有没有一种方法不涉及重写库的头或编写中间C包装器?
当然,您可以将整个c头包含在extern
语句中:
extern "C" {
#include "my_c_lib.h"
}
相关文章:
- 无法使用C风格函数调用验证签名
- 迫使QbyTearray将通过的C风格阵列拥有其构造函数
- C++风格的构造函数调用
- 我正在收到错误的“ list :: ShowList”:功能风格的初始化器似乎是我的CPP文件中的函数定义'
- C++函数和方法风格
- 如何在C#中实现C++风格的函数指针?,不使用代理
- C++11风格的SFINAE和模板实例化上的函数可见性
- 使用 lambda 函数定义非常小的帮助程序函数是一种好风格吗?
- K&R 风格的函数定义使用 g++ 编译
- C++宏中的函数式编程风格:这是否记录在任何地方
- 创建过于简单的函数是一种糟糕的风格吗
- 如何从C++中最好地调用具有C99风格数组函数签名的函数
- 什么时候无状态类函子可以用来代替c风格的函数
- 比较两个C风格字符串的正确函数是什么
- 如何让c风格的字符串函数工作
- 如何在c++中编写一个调试宏来打印函数的所有参数(strace风格)
- c++中的构造函数调用或函数风格强制转换
- c++枚举类型可以作为函数调用吗?或者它只是一种不同风格的类型转换
- c++中C99风格的变长数组函数签名
- 转换需要reinterpret_cast、c风格强制转换或函数风格强制转换