Skip to content

Commit

Permalink
Bug 1319673 - Make it compile error when instantiate a Variant with d…
Browse files Browse the repository at this point in the history
…uplicate type. r=Tomcat,Waldo

MozReview-Commit-ID: IKoLU2FjtzA
  • Loading branch information
JamesWCCheng committed Nov 23, 2016
1 parent 5e7f687 commit 4ecb740
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions mfbt/Variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ class Variant;

namespace detail {

template <typename...>
struct FirstTypeIsInRest;

template <typename First>
struct FirstTypeIsInRest<First> : FalseType {};

template <typename First, typename Second, typename... Rest>
struct FirstTypeIsInRest<First, Second, Rest...>
{
static constexpr bool value =
IsSame<First, Second>::value ||
FirstTypeIsInRest<First, Rest...>::value;
};

template <typename...>
struct TypesAreDistinct;

template <>
struct TypesAreDistinct<> : TrueType { };

template<typename First, typename... Rest>
struct TypesAreDistinct<First, Rest...>
{
static constexpr bool value =
!FirstTypeIsInRest<First, Rest...>::value &&
TypesAreDistinct<Rest...>::value;
};

// MaxSizeOf computes the maximum sizeof(T) for each T in Ts.

template<typename T, typename... Ts>
Expand Down Expand Up @@ -428,6 +456,7 @@ struct AsVariantTemporary
template<typename... Ts>
class MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS Variant
{
static_assert(detail::TypesAreDistinct<Ts...>::value, "Variant with duplicate types is not supported");
using Tag = typename detail::VariantTag<Ts...>::Type;
using Impl = detail::VariantImplementation<Tag, 0, Ts...>;
using RawData = AlignedStorage<detail::MaxSizeOf<Ts...>::size>;
Expand Down

0 comments on commit 4ecb740

Please sign in to comment.