我正在为一些C++代码编写C接口,我想知道哪些C++特性可以在外部C块中使用.
目前我使用的是
#ifdef __cplusplus
extern "C" {
#endif //__cplusplus
// Interface functions
#ifdef __cplusplus
}
#endif //__cplusplus
当使C接口可以在C和C++中使用时.
我想知道的具体C++特性有
- 命名空间
#ifdef __cplusplus
、namespace ... {
、... - 枚举课程
#ifdef __cplusplus
、#define ENUM enum class
、... - 在 struct 中添加成员类型,即
using
(同样是有条件的) -
noexcept
(同样是有条件的) - 内联类方法(同样是有条件的)
EDIT个
一个澄清的例子
#ifdef __cplusplus
#define CPP_ONLY( stuff ) stuff
#else
#define CPP_ONLY( stuff )
#endif //__cplusplus
#ifdef __cplusplus
extern "C" {
#endif //__cplusplus
CPP_ONLY( namespace Example { ) //(1)
enum CPP_ONLY( class ) Directions { //(2)
Left, Right
}
struct IntArray
{
size_t mSize;
int * mArray;
CPP_ONLY( using value_type = int; ) //(3)
CPP_ONLY( inline size_t size() { return mSize; } ) //(5)
}
int HelloWorld() CPP_ONLY( noexcept ); //(4)
CPP_ONLY( } ) // close namespace
#ifdef __cplusplus
}
#endif //__cplusplus
这CPP_ONLY
个代码中是否有任何代码会在以下位置导致编译/链接问题:
- 代码的c++实现
- 标题的c++内部用法
- 在c++中使用标头.