Skip to content

Commit

Permalink
default ctor : initialise with default contructed first type in param…
Browse files Browse the repository at this point in the history
…eters pack
  • Loading branch information
artemp committed Jun 12, 2014
1 parent 2ebabd9 commit 4d038f1
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ struct type_traits;
template <typename T, typename First, typename...Types>
struct type_traits<T, First, Types...>
{
static constexpr std::size_t id = std::is_same<T, First>::value ? sizeof...(Types) : type_traits<T, Types...>::id;
static constexpr std::size_t id = std::is_same<T, First>::value
? sizeof...(Types) : type_traits<T, Types...>::id;
};

template <typename T>
Expand All @@ -59,13 +60,32 @@ struct is_valid_type;
template <typename T, typename First, typename... Types>
struct is_valid_type<T,First,Types...>
{
static constexpr bool value = std::is_same<T,First>::value
static constexpr bool value = std::is_same<T, First>::value
|| is_valid_type<T,Types...>::value;
};

template <typename T>
struct is_valid_type<T> : std::false_type {};

template <std::size_t N, typename ... Types>
struct select_type
{
static_assert(N < sizeof...(Types), "index out of bounds");
};

template <std::size_t N, typename T, typename ... Types>
struct select_type<N,T,Types...>
{
typedef typename select_type<N - 1, Types...>::type type;
};

template <typename T, typename ... Types>
struct select_type<0, T, Types...>
{

typedef T type;
};

}

// static visitor
Expand Down Expand Up @@ -197,7 +217,7 @@ struct dispatcher<F,V>
typedef typename F::result_type result_type;
VARIANT_INLINE static result_type apply(V const&, F)
{
throw std::runtime_error("unary dispatch: FAIL");
throw std::runtime_error(std::string("unary dispatch: FAIL ") + typeid(V).name());
}
};

Expand Down Expand Up @@ -378,8 +398,10 @@ class variant
public:

VARIANT_INLINE variant()
: type_index(detail::invalid_value) {}

: type_index(0)
{
new (&data) typename detail::select_type<0,Types...>::type();
}

template <typename T,class = typename std::enable_if<
detail::is_valid_type<T,Types...>::value>::type>
Expand Down Expand Up @@ -422,6 +444,25 @@ class variant
return *this;
}

// conversions
// move-assign
//template <typename T>
//VARIANT_INLINE variant<Types...>& operator= (T && rhs) noexcept
//{
// variant<Types...> temp(std::move(rhs));
// swap(*this, temp);
// return *this;
//}

// copy-assign
//template <typename T>
//VARIANT_INLINE variant<Types...>& operator= (T const& rhs)
//{
// variant<Types...> temp(rhs);
// swap(*this, temp);
// return *this;
//}

template<typename T>
VARIANT_INLINE bool is() const
{
Expand Down

0 comments on commit 4d038f1

Please sign in to comment.