Commit 160f868c authored by stryku's avatar stryku Committed by Facebook Github Bot

Prevent IsOneOf unused template specialization instantiation

Summary:
Current `IsOneOf` implementation does unnecessary work because it instantiates all of the possible template specializations, even if type is same as the first of the tested ones. E.g.
`IsOneOf<char, char, int, float>` will instantiate:
```
IsOneOf<char, char, int, float>
IsOneOf<char, char, int>
IsOneOf<char, char>
IsOneOf<char>
```

With the proposed inheritance, compiler will stop initializing at the first match.
Closes https://github.com/facebook/folly/pull/643

Reviewed By: ericniebler

Differential Revision: D5482783

Pulled By: yfeldblum

fbshipit-source-id: 3d04c750ce72fa9b19b4d0588cccfb396a9e0715
parent 45245cb2
...@@ -387,7 +387,13 @@ struct Bools { ...@@ -387,7 +387,13 @@ struct Bools {
// Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly. // Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly.
template <class... Ts> template <class... Ts>
struct StrictConjunction struct StrictConjunction
: std::is_same<Bools<Ts::value..., true>, Bools<true, Ts::value...>> {}; : std::is_same<Bools<Ts::value...>, Bools<(Ts::value || true)...>> {};
template <class... Ts>
struct StrictDisjunction
: Negation<
std::is_same<Bools<Ts::value...>, Bools<(Ts::value && false)...>>
> {};
} // namespace folly } // namespace folly
...@@ -509,15 +515,8 @@ struct IsRelocatable< std::pair<T, U> > ...@@ -509,15 +515,8 @@ struct IsRelocatable< std::pair<T, U> >
IsRelocatable<U>::value> {}; IsRelocatable<U>::value> {};
// Is T one of T1, T2, ..., Tn? // Is T one of T1, T2, ..., Tn?
template <class T, class... Ts> template <typename T, typename... Ts>
struct IsOneOf { using IsOneOf = StrictDisjunction<std::is_same<T, Ts>...>;
enum { value = false };
};
template <class T, class T1, class... Ts>
struct IsOneOf<T, T1, Ts...> {
enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value };
};
/* /*
* Complementary type traits for integral comparisons. * Complementary type traits for integral comparisons.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment