我使用
template< TTL_TPARAMS_DEF(TTL_MAX_TYPELIST_PARAMS, empty_type) >
struct variant
{
typedef variant this_t;
typedef meta::typelist< TTL_ARGS(TTL_MAX_TYPELIST_PARAMS) > list;
...
template< typename T >
variant( const T& r ) : which_(0), pnt_(0)
{
typedef meta::find_equivalent_type<const T&, list> found;
pnt_ = new(stor_.buf_) ttl::data_holder<typename found::type>(r);
which_ = found::index;
}
...
private:
template<int N>
struct storage
{
union
{
ttl::data_holder_base dummy; //hope to satisfy alignment settings
char buf_[N];
};
};
int which_;
ttl::data_holder_base* pnt_;
storage< sizeof(ttl::data_holder<typename list::largest_type>) > stor_;
....
};
struct data_holder_base {};
template< typename T >
struct data_holder : data_holder_base
{
...
typedef const T& param_type;
T d;
...
data_holder( param_type d_ ) : d(d_) {}
...
};
当我在网络上发送这种类型的物体并用“memcpy”“重建”它时, 指针“pnt_”显然指向Nirvana。 由于我知道储存的类型, 我试图用一个投影来重建指针“pnt_” :
template<typename T>
inline void rebuild()
{
pnt_ = reinterpret_cast<ttl::data_holder<T>*>(stor_.buf_);
}
For the case I checked this example it works. But I don t know how the placement new (pnt_ = new(stor_.buf_) ...
) is placing the object into stor_.buf_
.
Would it be required to store something like std::distance(&stor_.buf_[0], pnt_)
to find the object?
还是有不同的方法可以把 pnt/code> 调回来?
Thank you Mario