Bug 1142366 - Add an equivalent of std::make_pair for mozilla::Pair. r=waldo

This commit is contained in:
Seth Fowler 2015-03-12 17:44:28 -07:00
Родитель e028338c3b
Коммит fcf3c149c4
2 изменённых файлов: 40 добавлений и 0 удалений

Просмотреть файл

@ -160,6 +160,10 @@ public:
: Base(Forward<AArg>(aA), Forward<BArg>(aB))
{}
Pair(Pair&& aOther)
: Base(Move(aOther.first()), Move(aOther.second()))
{ }
/** The A instance. */
using Base::first;
/** The B instance. */
@ -179,6 +183,26 @@ Swap(Pair<A, B>& aX, Pair<A, B>& aY)
aX.swap(aY);
}
/**
* MakePair allows you to construct a Pair instance using type inference. A call
* like this:
*
* MakePair(Foo(), Bar())
*
* will return a Pair<Foo, Bar>.
*/
template<typename A, typename B>
Pair<typename RemoveCV<typename RemoveReference<A>::Type>::Type,
typename RemoveCV<typename RemoveReference<B>::Type>::Type>
MakePair(A&& aA, B&& aB)
{
return
Pair<typename RemoveCV<typename RemoveReference<A>::Type>::Type,
typename RemoveCV<typename RemoveReference<B>::Type>::Type>(
Forward<A>(aA),
Forward<B>(aB));
}
} // namespace mozilla
#endif /* mozilla_Pair_h */

Просмотреть файл

@ -5,7 +5,10 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Pair.h"
#include "mozilla/TypeTraits.h"
using mozilla::IsSame;
using mozilla::MakePair;
using mozilla::Pair;
// Sizes aren't part of the guaranteed Pair interface, but we want to verify our
@ -59,5 +62,18 @@ struct OtherEmpty : EmptyClass { explicit OtherEmpty(int aI) : EmptyClass(aI) {}
int
main()
{
A a(0);
B b(0);
const A constA(0);
const B constB(0);
// Check that MakePair generates Pair objects of the correct types.
static_assert(IsSame<decltype(MakePair(A(0), B(0))), Pair<A, B>>::value,
"MakePair should strip rvalue references");
static_assert(IsSame<decltype(MakePair(a, b)), Pair<A, B>>::value,
"MakePair should strip lvalue references");
static_assert(IsSame<decltype(MakePair(constA, constB)), Pair<A, B>>::value,
"MakePair should strip CV-qualifiers");
return 0;
}