c++ - 是否可以创建一个宏来在 C/C++ 中创建一致的函数声明?

标签 c++ function macros c-preprocessor

基本上我想创建一个宏说

DECLARE_FUNC(name, arg1)

这将定义函数的名称和参数的名称。

我曾尝试执行以下操作但失败了

#define DECLARE_FILTER_FUNC(fname, arg1) (PointCloud<PointXYZ>::Ptr fname(PointCloud<PointXYZ>::Ptr arg1))

然后我这样定义函数

DECLARE_FILTER_FUNC(filterStatOutlierRemoval, inputCloud)
{
    return inputCloud;
}

我期待它扩展到

PointCloud<PointXYZ>::Ptr orcFilterStatOutlierRemoval(PointCloud<PointXYZ>::Ptr inputCloud)
{
    return inputCloud;
}

当我编译时我得到

error: expected constructor, destructor, or type conversion before ‘(’ token

我不确定我做错了什么,但我想做的是声明一组具有相同声明的过滤器函数,这样我就可以将它们作为函数指针传递,以获得更通用的功能。

最佳答案

从宏中去掉多余的括号。

#define DECLARE_FILTER_FUNC(fname, arg1) PointCloud<PointXYZ>::Ptr fname(PointCloud<PointXYZ>::Ptr arg1)

关于c++ - 是否可以创建一个宏来在 C/C++ 中创建一致的函数声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20534718/

相关文章:

c++ - 将 std::string_view 与需要以空字符结尾的字符串的 api 一起使用

c++ - 一次通过 O(n) 时间从大量 URL 列表中查找唯一的 URL

function - 返回 true/false 的 PowerShell 函数

c++ - g++ 在宏上提示 "does not give a valid preprocessing token"而 vc++ 编译正常

macros - 在 SAS 中自动扫描和搜索表/对象名称

C++在宏中获取宏名称

c++ - SDL 混合导致不正确的帧插值

c++ - 将 GTest 集成到我的项目时 gtest-printers.h 出错

javascript:通过类名调用函数

jquery - 如何按顺序运行两个不同的 jquery 函数?