如何在编译时检测类的声明

How to detect declaration of class at compile-time?

本文关键字:声明 检测 编译      更新时间:2023-10-16

我在项目中使用了一些第三方库。在更新到库的新版本后,我遇到了错误。

我的一个班有方法

virtual RTSPServer::RTSPClientSession* createNewClientSession(u_int32_t sessionId)override;

但在新版本的库声明中,RTSPClientSession被移到另一个类中并重命名。现在正确的名称是

GenericMediaServer::ClientSession

我需要一个能正确编译所有版本库的代码。

在gcc,我使用以下代码:

#ifdef RTSPServer::RTSPClientSession
    using ClientSessionClass = RTSPServer::RTSPClientSession;
#else
    using ClientSessionClass = GenericMediaServer::ClientSession;
#endif
class A
{
    .........
    virtual ClientSessionClass* createNewClientSession(u_int32_t sessionId)override;
};

但这在MSVC 2010中不起作用。如何检测应该使用哪个声明?

UPD:gcc的代码也不适用于旧版本的库:(

"如何检测我应该使用哪个声明?">

您需要为当前构建配置中使用的库版本引入一些鉴别器:

#ifdef OLD_LIBRARY_VERSION // Maybe the binding headers have some information like this.
                           // If not you have to select this manually, and provide the 
                           // setting with your project configuration.
   using ClientSessionClass = RTSPServer::RTSPClientSession;
#else
   using ClientSessionClass = GenericMediaServer::ClientSession;
#endif