Skip to content

Commit

Permalink
const as const can. also reimplementation of operator= to allow for A…
Browse files Browse the repository at this point in the history
…DL in swap
  • Loading branch information
DennisOSRM committed Feb 12, 2014
1 parent cd81aed commit 3360311
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct variant_helper<T, Types...>
}
}

VARIANT_INLINE static void move(std::size_t old_t, void * old_v, void * new_v)
VARIANT_INLINE static void move(const std::size_t old_t, void * old_v, void * new_v)
{
if (old_t == sizeof...(Types))
{
Expand All @@ -97,7 +97,7 @@ struct variant_helper<T, Types...>
}
}

VARIANT_INLINE static void copy(std::size_t old_t, const void * old_v, void * new_v)
VARIANT_INLINE static void copy(const std::size_t old_t, const void * old_v, void * new_v)
{
if (old_t == sizeof...(Types))
{
Expand All @@ -112,9 +112,9 @@ struct variant_helper<T, Types...>

template<> struct variant_helper<>
{
VARIANT_INLINE static void destroy(std::size_t, void *) {}
VARIANT_INLINE static void move(std::size_t, void *, void *) {}
VARIANT_INLINE static void copy(std::size_t, const void *, void *) {}
VARIANT_INLINE static void destroy(const std::size_t, void *) {}
VARIANT_INLINE static void move(const std::size_t, void *, void *) {}
VARIANT_INLINE static void copy(const std::size_t, const void *, void *) {}
};

namespace detail {
Expand Down Expand Up @@ -200,20 +200,26 @@ struct variant
helper_t::move(old.type_id, &old.data, &data);
}

VARIANT_INLINE variant<Types...>& operator= (variant<Types...> old)
friend void swap(variant<Types...> & first, variant<Types...> & second)
{
std::swap(type_id, old.type_id);
std::swap(data, old.data);
using std::swap; //enable ADL
swap(first.type_id, second.type_id);
swap(first.data, second.data);
}

VARIANT_INLINE variant<Types...>& operator= (variant<Types...> other)
{
swap(*this, other);
return *this;
}

template<typename T>
VARIANT_INLINE void is()
VARIANT_INLINE void is() const
{
return (type_id == detail::type_traits<T, Types...>::id);
}

VARIANT_INLINE void valid()
VARIANT_INLINE void valid() const
{
return (type_id != detail::invalid_value);
}
Expand Down Expand Up @@ -269,6 +275,7 @@ struct variant
{
helper_t::destroy(type_id, &data);
}

};

// unary visitor interface
Expand Down

0 comments on commit 3360311

Please sign in to comment.