简体   繁体   English

在 c++/c++11 中测试“POD-ness”?

[英]Test for “POD-ness” in c++/c++11?

I have some code which takes a packed POD structure/class and copies it into a memory block.我有一些代码采用打包的 POD 结构/类并将其复制到内存块中。

struct A
{
   int a;
   int b;
} a;

memcpy(mymemoryblock, (void *)&a, sizeof(A));

// later I get a reply and...

memcpy((void *)&a, mymemoryblock, sizeof(A));

This is only valid for POD types of data, and what I would like to know if there is a way I can test for POD-ness.这仅对 POD 类型的数据有效,我想知道是否有一种方法可以测试 POD 性。 If someone accidentally adds a member function to this class, the memcpy operations become invalid, but still compilable.如果有人不小心向此类添加了成员​​函数,则 memcpy 操作将无效,但仍可编译。 This leads to very difficult to detect bugs.这导致很难检测到错误。

Is there a is_POD_type(A) function, or some other trick that can be used to detect PODness at runtime or compile time?是否有 is_POD_type(A) 函数或其他一些可用于在运行时或编译时检测 PODness 的技巧?

std::is_pod<A>::value in C++11. C++11 中的std::is_pod<A>::value

[Edit: refer to Luc's comment above, in C++11 you don't need the type to be POD for what you're doing. [编辑:请参阅上面 Luc 的评论,在 C++11 中,您不需要将类型设为 POD 来完成您正在做的事情。

For that matter you also don't need to cast to void* , and C-style casting pointers to void* unnecessarily is a tiny bit risky, because some day you'll cast away const by accident!]就此而言,您也不需要强制转换为void* ,并且不必要地将 C 风格的指针强制转换为void*有点冒险,因为有一天您会意外地抛弃const !]

In C++03 there's no standard way to do it, but Boost has its own is_pod that errs on the side of caution on compilers that don't provide a non-standard way to find out.在 C++03 中,没有标准的方法可以做到这一点,但 Boost 有自己的is_pod ,它会在不提供非标准方法的编译器上谨慎行事。 So it's useful if you're writing code where the POD special case is an optimization (you just won't get the optimization everywhere).因此,如果您正在编写 POD 特殊情况是优化的代码(您不会在任何地方都得到优化),它会很有用。 It's also useful if you only care about compilers for which Boost can get an accurate answer.如果您只关心 Boost 可以得到准确答案的编译器,它也很有用。 It's not so good if false negatives by is_pod cause your code to give up in disgust.如果is_pod假阴性导致您的代码厌恶地放弃, is_pod了。

The standard (C++98) says that only types with C-like construction/destruction semantics can be members of a union.标准 (C++98) 规定只有具有类似 C 的构造/销毁语义的类型才能成为联合的成员。 That covers most of the things that would make a type non-POD, so just define a union type with a member of type A and the compiler should complain if A is not POD.这涵盖了使类型非 POD 的大部分内容,因此只需定义一个具有类型 A 成员的联合类型,如果 A 不是 POD,编译器应该会抱怨。

There is exist a method to call std::tr1::is_pod存在调用 std::tr1::is_pod 的方法

Also you can use bycicle like:你也可以使用 bycicle 像:

#define CHECK_TYPE_IS_A_POD(TYPE)\
{\
  switch(1)\
  {\
    case 1:\
        TYPE IF_COMPILE_ERROR_THEN__##TYPE##__IS_NOT_A_POD;\
        /* prune out any warnings about not usage */ \
        IF_COMPILE_ERROR_THEN__##TYPE##__IS_NOT_A_POD = TYPE();\
    case 2:\
        ;\
  }\

But it doen't work for namespace qualification names and specialized template types.但它不适用于命名空间限定名称和专用模板类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM