Boost logo

Boost Users :

Subject: Re: [Boost-users] Problem with Interpocess offset_ptr
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2011-12-29 04:36:17


El 26/12/2011 11:49, mike_at_tesla.rcub.bg.ac.rs escribió:
> Hi,
> I need to store in the shared memory structure for Quadtree.
> It is a class with 4 pointers for tree regions and vector of points:
> class CNode4
> {
> ...
> CNode4* node0;
> CNode4* node1;
> CNode4* node2;
> CNode4* node3;
> vector<vec2> pointArray;
> };
>
> I have tried with
> class CNode4
> {
> ...
> offset_ptr<CSNode4> node0;
> ...
> };
>
> and using as:
> typedef allocator<void, managed_shared_memory::segment_manager>
> void_allocator;
> const void_allocator alloc_void(segment.get_segment_manager());
> CSNode4* rootNode = segment.construct<CSNode4> (_RootNode) (alloc_void);
> rootNode->node0 = new CSNode4(alloc_void,...);
>
> this works until I try to static_cast node0
> offset_ptr<CSNode4> N1 = static_cast<CSNode4*>(rootNode->node0);
> I get:
> (MSVC 2010, boost 1.42)
> error C2440: 'static_cast' : cannot convert from
> 'boost::interprocess::offset_ptr<PointedType>' to 'CSNode4 *'

You can't static cast between offset_ptrs (language limitations,
supporting static_cast would allow unwated conversions). But you can
convert offset_ptr to a raw pointer, static cast it and assign it to a
offset_ptr again:

offset_ptr<CSNode4> N1 = static_cast<CSNode4*>(rootNode->node0.get());

BTW, in C++11 there is a standard class (pointer_traits) to have a
uniform interface to any pointer, but sadly it does not support castings.

Best,

Ion


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net