Inward conversions and helpers for a generalized union type.

From Mechanism to Method
Valued Conversions

Kevlin Henney
Listing 2. Inward conversions and helpers for a generalized union type.


class any
{
public:
  ...
  any(const any &other)
    : content(other.content ? other.content->clone() : 0)
  {
  }
  template<typename value_type>
  any(const value_type &value)
    : content(new holder<value_type>(value))
  {
  }
  any &swap(any &rhs)
  {
      std::swap(content, rhs.content);
      return *this;
  }
  any &operator=(const any &rhs)
  {
      return swap(any(rhs));
  }
  template<typename value_type>
  any &operator=(const value_type &rhs)
  {
      return swap(any(rhs));
  }
  ...
};