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));
  }
  ...
};

Upcoming Training Events

0 AM
Visual Studio Live! San Diego
September 8-12, 2025
Live! 360 Orlando
November 16-21, 2025
Cloud & Containers Live! Orlando
November 16-21, 2025
Data Platform Live! Orlando
November 16-21, 2025
Visual Studio Live! Orlando
November 16-21, 2025