Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 42 additions & 29 deletions src/variant17.lib/variant17/Variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,53 @@ auto selectType() {
template<size_t N>
using SelectType = UnwrapType<decltype(selectType<N>())>;

/// Variant != std::variant
/// * unchecked invalid state (only destruction is valid!)
/// * simple recursive vistor
/// * allows uncheck casts (you have to check before!)
/// * sizeof(index) limits
/// VariantWhich is an enum-like type used in Variant to determine which type is currently present in the Variant.
template<class... Ts>
struct Variant {
struct VariantWhich {
static constexpr auto pack = to_type_pack<Ts...>;
static constexpr auto indices = indexPackFor(pack);
using First = TypeHead<decltype(pack)>;
using WhichValue = UnwrapType<SelectType<sizeof...(Ts)>>; // enough for npos!
enum { npos = sizeof...(Ts) }; // invalid state after exception - only destruction checks!

struct Which {
explicit constexpr Which(WhichValue v)
: value(v) {}
constexpr VariantWhich() = default;
explicit constexpr VariantWhich(WhichValue v)
: value(v) {}

constexpr operator WhichValue() const { return value; }
constexpr operator WhichValue() const { return value; }

constexpr bool operator==(Which w) const { return w.value == value; }
constexpr bool operator!=(Which w) const { return w.value != value; }
constexpr bool operator==(VariantWhich w) const { return w.value == value; }
constexpr bool operator!=(VariantWhich w) const { return w.value != value; }

template<class T>
constexpr bool operator==(Type<T>) const {
return whichOf<T>() == *this;
}
template<class T>
constexpr bool operator!=(Type<T>) const {
return whichOf<T>() != *this;
}
template<class T>
constexpr bool operator==(Type<T>) const {
return of<T>() == *this;
}
template<class T>
constexpr bool operator!=(Type<T>) const {
return of<T>() != *this;
}

private:
WhichValue value;
};
template<class T>
constexpr static auto of(Type<T> = {}) -> VariantWhich {
return VariantWhich{static_cast<WhichValue>(indexedTypePackIndexOf<T>(pack, indices))};
}

private:
WhichValue value{};
};

/// Variant != std::variant
/// * unchecked invalid state (only destruction is valid!)
/// * simple recursive vistor
/// * allows uncheck casts (you have to check before!)
/// * sizeof(index) limits
template<class... Ts>
struct Variant {
using Which = VariantWhich<Ts...>;
static constexpr auto pack = Which::pack;
static constexpr auto indices = Which::indices;
using First = TypeHead<decltype(pack)>;
using WhichValue = typename Which::WhichValue;
enum { npos = sizeof...(Ts) }; // invalid state after exception - only destruction checks!

private:
std::aligned_union_t<0, Ts...> m{};
Expand Down Expand Up @@ -171,15 +184,15 @@ struct Variant {
Variant(T&& t) {
static_assert(containsOf<BT>(pack), "type not part of variant");
constructOf(type<BT>, std::forward<T>(t));
whichValue = whichOf<BT>();
whichValue = whichOf(type<BT>);
}

/// inplace construct of type
template<class T, class... Args>
Variant(Type<T>, Args&&... args) {
static_assert(containsOf<T>(pack), "type not part of variant");
constructOf(type<T>, std::forward<Args>(args)...);
whichValue = whichOf<T>();
whichValue = whichOf(type<T>);
}

template<size_t I, class... Args>
Expand Down Expand Up @@ -207,12 +220,12 @@ struct Variant {
if (whichValue != npos) destruct();
whichValue = npos;
constructOf(type<T>, std::forward<Args>(args)...);
whichValue = whichOf<T>();
whichValue = whichOf(type<T>);
}

template<class T>
constexpr static auto whichOf(Type<T> = {}) -> Which {
return Which{static_cast<WhichValue>(indexedTypePackIndexOf<T>(pack, indices))};
return Which::of(type<T>);
}

template<size_t I>
Expand Down
6 changes: 6 additions & 0 deletions src/variant17.lib/variant17/Variant.trait.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ constexpr auto is_variant = false;
template<class... Ts>
constexpr auto is_variant<Variant<Ts...>> = true;

template<class T>
constexpr auto is_variant_which = false;

template<class... Ts>
constexpr auto is_variant_which<VariantWhich<Ts...>> = true;

} // namespace variant17
7 changes: 7 additions & 0 deletions src/variant17.lib/variant17/Variant.trait.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ using namespace variant17;
TEST(Variant, trait) {
using T = Variant<int, double, char, short, bool>;
static_assert(is_variant<T>);
static_assert(!is_variant_which<T>);

static_assert(!is_variant<T::Which>);
static_assert(is_variant_which<T::Which>);

struct S {
int i;
};
static_assert(!is_variant<S>);
static_assert(!is_variant_which<S>);

using P = std::pair<int, double>;
static_assert(!is_variant<P>);
static_assert(!is_variant_which<P>);

static_assert(!is_variant<int>);
static_assert(!is_variant_which<int>);
}