c++ - C++ 中函数或类之前的宏是什么?

标签 c++

<分区>

我见过这样的代码:

#define A // macro
A void foo(bar); // function declaration

还有这个:

#define B // macro
class B foo { // class declaration
  bar
};

在那里使用宏是什么意思?

呃...我的意思是我不明白语法。我以前没见过这个。

其实我只是在opencv3.1的features2d.hpp中找到了这种代码。

class CV_EXPORTS_W BOWImgDescriptorExtractor {
...
CV_WRAP void setVocabulary( const Mat& vocabulary );
...
}

在cvdef.h中

#if (defined WIN32 || defined _WIN32 || defined WINCE || defined __CYGWIN__) && defined CVAPI_EXPORTS
#  define CV_EXPORTS __declspec(dllexport)
#elif defined __GNUC__ && __GNUC__ >= 4
#  define CV_EXPORTS __attribute__ ((visibility ("default")))
#else
#  define CV_EXPORTS
#endif

/* special informative macros for wrapper generators */
#define CV_EXPORTS_W CV_EXPORTS
#define CV_WRAP

这里,CV_EXPORTS_W 和 CV_WRAP 是宏。我在 C++ 中没有看到这种语法。

最佳答案

通常这些东西是编译器或系统特定语言扩展的占位符。

例如,如果使用 Windows DLL 构建程序,要导入符号,可以声明函数

__declspec(dllimport) void foo();

问题是,如果代码被移植到另一个系统,或者使用不支持此类非标准功能的编译器构建,__declspec(dllimport) 通常将无法工作。

因此,相反,声明可能是

#ifdef _WIN32    /*  or some macros specific to a compiler targeting windows */

#define IMPORT __declspec(dllimport)
#else
#define IMPORT
#endif

IMPORT void foo();

在很多情况下,对于特定的编译器、目标(例如,从库中导入符号的程序,或正在构建的库以导出符号),以及可能使用此类技术的主机系统。

关于c++ - C++ 中函数或类之前的宏是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34495206/

相关文章:

c++ - 如何将复杂的 C++ XCode 项目转换为 Cocoa 应用程序?

c++ - 在 C++ 中使用 cURL 的 JSON 请求

c++ 检查整数是否在数组中

c++ - const 指针作为 std::bind 参数

.net - C++ 编译器是否可以跨 DLL 内联方法调用?

c++ - 为什么析构函数默认不是虚拟的[C++]

使用框架绘制 Sprite 时C++ Sfml白色方 block

c++ - “Could not determine which ” 制作 “command to run. Check the ” 制作 “step in the build configuration.” Qt 创建者

c++ - Qt中如何编写Sqlite数据库

c++ - 在相反方向上具有相同常数的C++移位,在较小的代码更改下结果不同