This question already has answers here:
Macros in the middle of a class or function declaration

(4个答案)


4年前关闭。




我发现以下代码:
class BOOST_FILESYSTEM_DECL path
{

};

通常,类是通过以下方式定义的:
class Baloon
{

};

在类声明中如何有两个术语?有人知道BOOST_FILESYSTEM_DECL用于什么(增强库)吗?

最佳答案

如果您看看how it is defined,那么一切都会很清楚:

#ifdef BOOST_HAS_DECLSPEC // defined in config system
// we need to import/export our code only if the user has specifically
// asked for it by defining either BOOST_ALL_DYN_LINK if they want all boost
// libraries to be dynamically linked, or BOOST_FILESYSTEM_DYN_LINK
// if they want just this one to be dynamically liked:
#if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_FILESYSTEM_DYN_LINK)
// export if this is our own source, otherwise import:
#ifdef BOOST_FILESYSTEM_SOURCE
# define BOOST_FILESYSTEM_DECL __declspec(dllexport)
#else
# define BOOST_FILESYSTEM_DECL __declspec(dllimport)
#endif  // BOOST_FILESYSTEM_SOURCE
#endif  // DYN_LINK
#endif  // BOOST_HAS_DECLSPEC
//
// if BOOST_FILESYSTEM_DECL isn't defined yet define it now:
#ifndef BOOST_FILESYSTEM_DECL
#define BOOST_FILESYSTEM_DECL
#endif

如您所见,它是一个宏。它扩展为__declspec(dllexport)__declspec(dllimport)或为空,具体取决于其他宏。有关decspec标识符的更多详细信息,请参见this answer。简而言之,动态链接需要平台特定的功能。

09-05 05:56