具体来说,我想确定 new T是什么?对于任何模板参数 T ,不同于 new T()。哪个类型特征 is_trivial , is_standard_layout 和 is_pod 我选择?
(作为一个侧面的问题,这些类型traits是否可以没有编译器魔法?)
解决方案我不认为它可以用真正的外行人的术语,至少没有一个很多额外的解释。一个重要的点是静态和动态初始化,但解释说,对一个外行人本身将是几个页面...
PODs(mis-) +98。实际上有两个单独的意图,没有表示得很好:1)如果你在C ++中编译一个C结构声明,你得到的应该是等同于你在C中。2)POD只会需要/使用静态(非动态)初始化。
C ++ 0x / 11完全丢弃了POD的名称,赞成琐碎和标准布局。标准布局旨在捕获第一个意图 - 使用与在C中获得的布局相同的布局创建一个。Trivial旨在捕获对静态初始化的支持。
由于新T 与新T()处理初始化,您可能 is_trivial 。
我不确定需要编译器魔法。我立即的反应可能是肯定的,但是知道一些人们用TMP做的事情,我很难确定有人不能这样做。
Edit:例如,最好只是引用N3290的例子:
struct N {//既不简单也不standard-layout int i; int j; virtual〜N(); }; struct T {//简单但不是标准布局 int i; private: int j; }; struct SL {//标准布局但不重要 int i; int j; 〜SL(); }; struct POD {//简单和标准布局 int i; int j; };无疑是猜测, POD 也是POD结构。
In layman's terms, what's the difference between trivial types, standard layout types and PODs?
Specifically, I want to determine whether new T is different from new T() for any template parameter T. Which of the type traits is_trivial, is_standard_layout and is_pod should I choose?
(As a side question, can any of these type traits be implemented without compiler magic?)
解决方案I don't think it can be done in truly layman's terms, at least without a lot of extra explanation. One important point is static vs. dynamic initialization, but explaining that to a layman would be several pages in itself...
PODs were (mis-)defined in C++98. There are really two separate intents involved, neither expressed very well: 1) that if you compile a C struct declaration in C++, what you get should be equivalent to what you had in C. 2) A POD will only ever need/use static (not dynamic) initialization.
C++0x/11 drops the "POD" designation (almost) entirely, in favor of "trivial" and "standard layout". Standard layout is intended to capture the first intent -- creating something with a layout the same as you'd get in C. Trivial is intended to capture the support for static initialization.
Since new T vs. new T() deals with initialization, you probably want is_trivial.
I'm not sure about compiler magic being required. My immediate reaction would be probably yes, but knowing some of the things people have done with TMP, I have a hard time being certain somebody couldn't do this too...
Edit: for examples, perhaps it's best to just quote the examples from N3290:
struct N { // neither trivial nor standard-layout int i; int j; virtual ~N(); }; struct T { // trivial but not standard-layout int i; private: int j; }; struct SL { // standard-layout but not trivial int i; int j; ~SL(); }; struct POD { // both trivial and standard-layout int i; int j; };As you can undoubtedly guess, POD is also a POD struct.
更多推荐
平凡vs.标准布局vs. POD
发布评论