如何在编译时检测类的声明
How to detect declaration of class at compile-time?
我在项目中使用了一些第三方库。在更新到库的新版本后,我遇到了错误。
我的一个班有方法
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
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 使用CMake检测支持的C++标准
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 当套接字连接断开时检测C/C++Unix
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- 为什么C++编译器没有检测到正确声明的类?
- 在编译器之前检测未声明的标识符
- 为什么 gcc 无法从其前向声明中检测到友元类命名空间?
- 是否有一种方法可以使用SFINAE来检测是否未声明非模板化的非成员函数
- 如何在编译时检测类的声明
- 如何检测C++中已声明但未定义的函数