提振玉米pressed_pa​​ir空物体的地址 [英] boost compressed_pair and addresses of empty objects

查看:125
本文介绍了提振玉米pressed_pa​​ir空物体的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,提振:: COM pressed_pa​​ir <​​/A>应该确保第一和第二memebrs的地址是不同的,同时完成其COM pressing一对神奇。在这里是这么说的。好像它是并非如此,它的行为是在不同的编译器不同。我使用升压v 1.47。我缺少什么?

 结构E1 {};
结构的E2 {};提高:: COM pressed_pa​​ir&LT; E1,E2&GT; diff_pair;
提高:: COM pressed_pa​​ir&LT; E1,E1&GT; same_pair;//铛++和g ++ 4.7打印相同的地址,但VC2010打印不同的地址。
的printf(对不同=%P,%P \\ N,&安培; diff_pair.first(),放大器; diff_pair.second());//铛++和g ++ 4.7打印不同的地址,但VC2010打印相同的地址。
的printf(对不同=%P,%P \\ N,&安培; same_pair.first(),放大器; same_pair.second());


在类型不同的种类与一个或两个是空类,子对象都应该是在同一个地址(如果编译器能拉空基类优化功能),这就是COM pressed对的点。

在类型都是一样的,我觉得从标准第10章的说明适用:


  

一个基类子对象可以是零尺寸(第9条)的;然而,二
  具有相同的类型和子对象属于同一
  最派生类对象不能在同一地址(5.10)进行分配。


所以看起来它是由编译器,以确保他们在不同的地址分配(VC10和可能得到它错误的)。

在升压的标题中的评论表明,较早他们根本没把同样的空类的两个不同的实例在com pressed对的。相反,他们只存储一个实例,既第()第二()返回同一个对象。

  // 4 T1 T2 = =,T1和T2两个空
   //本来这没有储存T2的一个实例,在所有
   //但导致的问题beause这意味着&安培; x.first()==&安培; x.second()
   //这是不是其他任何一种对真正的,所以现在我们店的一个实例
   以防万一T2的//用户是依靠第一()和第二()返回
   //不同的对象(虽然同时为空)。

AFAIK, boost::compressed_pair is supposed to ensure that the address of the first and second memebrs are different while it does its magic of compressing the pair. It says so here. Seems like it is not the case and its behavior is different on different compilers. I'm using boost v 1.47. What am I missing?

struct E1 {};
struct E2 {};

boost::compressed_pair<E1, E2> diff_pair;
boost::compressed_pair<E1, E1> same_pair;

// clang++ and g++ 4.7 print the same address but VC2010 prints different addresses.
printf("different pairs = %p, %p\n", &diff_pair.first(), &diff_pair.second());

// clang++ and g++ 4.7 print different addresses but VC2010 prints the same address.
printf("different pairs = %p, %p\n", &same_pair.first(), &same_pair.second());

解决方案

When the types are different and one or both of the types is an empty class, the subobjects are supposed to be at the same address (if the compiler can pull the empty base class optimization off), that's the point of the compressed pair.

When the types are the same, I think a note from chapter 10 in the standard applies:

A base class subobject may be of zero size (Clause 9); however, two subobjects that have the same class type and that belong to the same most derived object must not be allocated at the same address (5.10).

So it seems it is up to the compiler to ensure that they are allocated at different addresses (and VC10 might be getting it wrong).

The comments in the boost's header indicate, that earlier they didn't bother to put two different instances of the same empty class in the compressed pair at all. Instead they just stored one instance and both first() and second() returned the same object.

   // 4    T1 == T2, T1 and T2 both empty
   //      Originally this did not store an instance of T2 at all
   //      but that led to problems beause it meant &x.first() == &x.second()
   //      which is not true for any other kind of pair, so now we store an instance
   //      of T2 just in case the user is relying on first() and second() returning
   //      different objects (albeit both empty).

这篇关于提振玉米pressed_pa​​ir空物体的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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