【问题标题】:Is it possible to create a macro to create consistent function declarations in C/C++?是否可以创建一个宏来在 C/C++ 中创建一致的函数声明?
【发布时间】:2013-12-12 03:54:17
【问题描述】:

基本上我想创建一个宏说

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;
}

当我编译时我得到

错误:在“(”标记之前需要构造函数、析构函数或类型转换

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

【问题讨论】:

  • 为什么要把定义放在括号里?
  • 并且你应该在询问之前查看预处理器的输出。
  • 是的,我应该有。谢谢你的提醒。我很少发现需要。

标签: c++ function macros c-preprocessor


【解决方案1】:

从你的宏中去掉多余的括号。

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

【讨论】:

    猜你喜欢
    • 2020-03-31
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多