Skip to content

Commit

Permalink
allow explicit un-initilised variant ctor
Browse files Browse the repository at this point in the history
```c++
util::variant<int, double, std::string> var(util::no_init());
var.valid(); // false
util::variant<int, double, std::string>(util::no_init()).get_type_index() == util::detail::invalid_value; // true
```
  • Loading branch information
artemp committed Jul 2, 2014
1 parent 08a7e04 commit d99139d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ TEST_CASE( "variant default constructor", "[variant][default constructor]" ) {
// As a result first type in Types... must be default constructable
// NOTE: index in reverse order -> index = N - 1
REQUIRE((util::variant<int, double, std::string>().get_type_index() == 2));
REQUIRE((util::variant<int, double, std::string>(util::no_init()).get_type_index() == util::detail::invalid_value));
}

TEST_CASE( "variant visitation", "[visitor][unary visitor]" ) {
Expand Down
6 changes: 6 additions & 0 deletions variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ class printer : public static_visitor<>

} // namespace detail

struct no_init {};

template<typename... Types>
class variant
{
Expand All @@ -503,12 +505,16 @@ class variant

public:


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

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

template <typename T, class = typename std::enable_if<
detail::is_valid_type<T, Types...>::value>::type>
VARIANT_INLINE explicit variant(T const& val) noexcept
Expand Down

0 comments on commit d99139d

Please sign in to comment.