Boost logo

Boost :

From: Bill Wade (bill.wade_at_[hidden])
Date: 2000-09-14 16:28:35

> From: William Kempf [mailto:sirwillard_at_[hidden]]

> unsafe_lock<> isn't that tricky.

The first version posted wasn't portable. It assumed that an array of char
would be suitably aligned for a safe lock. In practice you can probably
force alignment with a union, and verify (at compile time) that you were
successful with alignment_of. However I do believe that is at least
moderately "tricky."

Perhaps some ambitious soul can give us


that would, for-instance, return double if the alignment_of<T> and
alignment_of<double> were the same.

With that you can write a non-heap container that holds zero or one
properly aligned and constructed instances of a type.

  template<class T> class Tpot
    T& object(){ return reinterpret_cast<T&>(object_space); }
    const T& object() const
    { return reinterpret_cast<const T&>(object_space); }

    // Are these const? For now I'll say no.
    void construct(){ new((void*)object()) T; }
    void destruct(){ object().~T(); }

    // Let derived classes implement these if they wish.
    // Not implemented here.
    Tpot(const Tpot&);
    void operator=(const Tpot&);

    union // anonymous
      char object_space[sizeof(T)];
      typename alignment_of<T>::pod_type alignment;

Boost list run by bdawes at, gregod at, cpdaniel at, john at