Skip to content

Commit

Permalink
is<T>() - add specialisation for recursive_wrapper<T> + update test…
Browse files Browse the repository at this point in the history
…s (ref #102)
  • Loading branch information
artemp committed May 11, 2016
1 parent 04dc3a4 commit c6ae1ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions include/mapbox/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,20 @@ class variant
return *this;
}

template <typename T>
template <typename T, typename std::enable_if<
(detail::direct_type<T, Types...>::index != detail::invalid_value)>::type* = nullptr>
VARIANT_INLINE bool is() const
{
static_assert(detail::has_type<T, Types...>::value, "invalid type in T in `is<T>()` for this variant");
return type_index == detail::direct_type<T, Types...>::index;
}

template <typename T,typename std::enable_if<
(detail::direct_type<recursive_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
VARIANT_INLINE bool is() const
{
return type_index == detail::direct_type<recursive_wrapper<T>, Types...>::index;
}

VARIANT_INLINE bool valid() const
{
return type_index != detail::invalid_value;
Expand Down
8 changes: 8 additions & 0 deletions test/t/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,11 @@ TEST_CASE("storing reference wrappers to consts works")
},
mapbox::util::bad_variant_access&);
}

TEST_CASE("recursive wrapper")
{
using variant_type = mapbox::util::variant<mapbox::util::recursive_wrapper<int>>;
variant_type v(1);
REQUIRE(v.is<int>());
REQUIRE(v.get<int>() == 1);
}

0 comments on commit c6ae1ea

Please sign in to comment.