Bug 1345842 - Add Ignore to allow ignore elements in Tie. r=froydnj

Differential Revision: https://phabricator.services.mozilla.com/D68742

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Chris Fronk 2020-03-31 12:36:22 +00:00
parent 50b327f08e
commit 62d6d7db0e
2 changed files with 43 additions and 0 deletions

View File

@ -67,6 +67,19 @@ struct CheckConvertibility<Group<SourceTypes...>, Group<TargetTypes...>>
sizeof...(SourceTypes) ==
sizeof...(TargetTypes)> {};
/*
* Helper type for Tie(args...) to allow ignoring specific elements
* during Tie unpacking. Supports assignment from any type.
*
* Not for direct usage; instead, use mozilla::Ignore in calls to Tie.
*/
struct IgnoreImpl {
template <typename T>
constexpr const IgnoreImpl& operator=(const T&) const {
return *this;
}
};
/*
* TupleImpl is a helper class used to implement mozilla::Tuple.
* It represents one node in a recursive inheritance hierarchy.
@ -463,6 +476,22 @@ inline Tuple<std::decay_t<Elements>...> MakeTuple(Elements&&... aElements) {
return Tuple<std::decay_t<Elements>...>(std::forward<Elements>(aElements)...);
}
/**
* A helper placholder to allow ignoring specific elements during Tie unpacking.
* Can be used with any type and any number of elements in a call to Tie.
*
* Usage of Ignore with Tie is equivalent to using std::ignore with
* std::tie.
*
* Example:
*
* int i;
* float f;
* char c;
* Tie(i, Ignore, f, c, Ignore) = FunctionThatReturnsATuple();
*/
constexpr detail::IgnoreImpl Ignore;
/**
* A convenience function for constructing a tuple of references to a
* sequence of variables. Since assignments to the elements of the tuple

View File

@ -281,6 +281,19 @@ static bool TestTie() {
return true;
}
static bool TestTieIgnore() {
int i;
char c;
Tuple<int, float, char> rhs1(42, 0.5f, 'c');
Tie(i, mozilla::Ignore, c) = rhs1;
CHECK(i == Get<0>(rhs1));
CHECK(c == Get<2>(rhs1));
return true;
}
int main() {
TestConstruction();
TestConstructionFromMozPair();
@ -291,6 +304,7 @@ int main() {
TestGet();
TestMakeTuple();
TestTie();
TestTieIgnore();
TestTieMozPair();
return 0;
}