C ++任何方式通过编程检测POD结构? [英] C++ Any way to programmatically detect POD-struct?

查看:134
本文介绍了C ++任何方式通过编程检测POD结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据结构存储POD结构(每个实例化只存储一个类型,因为它基本上是一个特定的POD结构的数组)。有时另一个dev。将修改其中一个这些结构,添加或修改数据类型。如果添加非POD元件, std :: string,数据结构在运行时爆炸,因为内存模型改变。有什么方法来检测类或结构是否符合POD标准使用编译器定义或在运行时调用(避免此维护问题)?我在使用g ++(GCC)4.2.4。

I have data structure which stores POD-structs (each instantiation stores a single type only, since it is basically an array of a specific POD-struct). Sometimes another dev. will modify one of these structs, adding or modifying a data type. If a non-POD element is added, e.g. std::string, the data structure blows-up at runtime, because the memory model changes. Is there any way to detect if a class or struct is POD-compliant using compiler defines or a call at run-time (to avoid this maintainence issue) ? I'm using g++ (GCC) 4.2.4.

推荐答案

在运行时可能不,但在编译时, is_pod trait来自C ++ 0x标准库或 Boost.TypeTraits

At runtime probably not, but at compile time, you can use is_pod trait from either C++0x standard library or Boost.TypeTraits.

static_assert(std::is_pod<YourStruct>::value);

这篇关于C ++任何方式通过编程检测POD结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆