Rob*_*tke 7 c++ metaprogramming type-traits c++11 c++14
我有课程:
struct A { // has no pointer members, POD - it's fine
int a, b;
char c;
};
struct B { // has no pointer members, but not POD - it's still fine
int a, b;
std::string s;
};
struct C { // has pointer members, it's not fine
int a,b;
char* cs;
};
Run Code Online (Sandbox Code Playgroud)
我需要在编译时检测是否有任何类具有 的属性struct C,即具有指针作为成员。
简短的推理:我需要确保用户定义的类型可以通过复制或赋值(例如)或通过在类中struct A提供用户定义的serialize()和方法(例如和)安全地序列化和反序列化到某个缓冲区。deserialize()struct Bstruct c
如果B或C没有实现这些方法,那么编译应该失败,但如果A没有这些方法,那么编译应该成功。
更新:
检查类是否有指针数据成员的解决方案仅适用于:
struct D {
int* p; // the pointer must be named 'p'
};
Run Code Online (Sandbox Code Playgroud)
这个问题是另一种情况。请问我们可以重新开放吗?
从 C++17 开始,这是不可能的。
您可以测试许多特征,但没有任何特征可以按照您需要的方式检查数据成员。std::is_pod<>(在 C++20 中将被弃用)是你能得到的最好的。