Eric Fiselier 13858ee056 [libcxx] Add <experimental/tuple> header for LFTS.
Summary:
This patch adds the `<experimental/tuple>` header (almost) as specified in the latest draft of the library fundamentals TS.

The main changes in this patch are:

1. Added variable template `tuple_size_v`
2. Added function `apply(Func &&, Tuple &&)`.
3. Changed `__invoke` to be `_LIBCPP_CONSTEXPR_AFTER_CXX11`.

The `apply(...)` implementation uses `__invoke` to invoke the given function. `__invoke` already provides the required functionality. Using `__invoke` also allows `apply` to be used on pointers to member function/objects as an extension. In order to facilitate this `__invoke` has to be marked `constexpr`. 



Test Plan:
Each new feature was tested. 

The test cases for `tuple_size_v` are as follows:
1. tuple_size_v.pass.cpp
  - Check `tuple_size_v` on cv qualified tuples, pairs and arrays.
2. tuple_size_v.fail.cpp
  - Test on reference type.
3. tuple_size_v_2.fail.cpp
  - Test on non-tuple
4. tuple_size_v_3.fail.cpp
  - Test on pointer type.

The test cases for tuple.apply are as follows:

1. arg_type.pass.cpp
   - Ensure that ref/pointer/cv qualified types are properly passed.
2. constexpr_types.pass.cpp
   - Ensure constexpr evaluation of apply is possible for `tuple` and `pair`.
3. extended_types.pass.cpp
   - Test apply on function types permitted by extension.
4. large_arity.pass.cpp
   - Test that apply can evaluated on tuples and arrays with large sizes.
5. ref_qualifiers.pass.cpp
   - Test that apply respects ref qualified functions.
6. return_type.pass.cpp
   - Test that apply returns the proper type.
7. types.pass.cpp
   - Test apply on function types as required by LFTS.

Reviewers: mclow.lists

Reviewed By: mclow.lists

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D4512

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@232515 91177308-0d34-0410-b5e6-96231b3b80d8
2015-03-17 18:28:14 +00:00

147 lines
3.6 KiB
C++

//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++98, c++03, c++11
// <experimental/tuple>
// template <class F, class T> constexpr decltype(auto) apply(F &&, T &&)
// Stress testing large arities with tuple and array.
#include <experimental/tuple>
#include <array>
#include <utility>
#include <cassert>
////////////////////////////////////////////////////////////////////////////////
template <class T, std::size_t Dummy = 0>
struct always_imp
{
typedef T type;
};
template <class T, std::size_t Dummy = 0>
using always_t = typename always_imp<T, Dummy>::type;
////////////////////////////////////////////////////////////////////////////////
template <class Tuple, class Idx>
struct make_function;
template <class Tp, std::size_t ...Idx>
struct make_function<Tp, std::integer_sequence<std::size_t, Idx...>>
{
using type = bool (*)(always_t<Tp, Idx>...);
};
template <class Tp, std::size_t Size>
using make_function_t = typename make_function<Tp, std::make_index_sequence<Size>>::type;
////////////////////////////////////////////////////////////////////////////////
template <class Tp, class Idx>
struct make_tuple_imp;
////////////////////////////////////////////////////////////////////////////////
template <class Tp, std::size_t ...Idx>
struct make_tuple_imp<Tp, std::integer_sequence<std::size_t, Idx...>>
{
using type = std::tuple<always_t<Tp, Idx>...>;
};
template <class Tp, std::size_t Size>
using make_tuple_t = typename make_tuple_imp<Tp, std::make_index_sequence<Size>>::type;
template <class ...Types>
bool test_apply_fn(Types...) { return true; }
namespace ex = std::experimental;
template <std::size_t Size>
void test_all()
{
using A = std::array<int, Size>;
using ConstA = std::array<int const, Size>;
using Tuple = make_tuple_t<int, Size>;
using CTuple = make_tuple_t<const int, Size>;
using ValFn = make_function_t<int, Size>;
ValFn val_fn = &test_apply_fn;
using RefFn = make_function_t<int &, Size>;
RefFn ref_fn = &test_apply_fn;
using CRefFn = make_function_t<int const &, Size>;
CRefFn cref_fn = &test_apply_fn;
using RRefFn = make_function_t<int &&, Size>;
RRefFn rref_fn = &test_apply_fn;
{
A a{};
assert(ex::apply(val_fn, a));
assert(ex::apply(ref_fn, a));
assert(ex::apply(cref_fn, a));
assert(ex::apply(rref_fn, std::move(a)));
}
{
ConstA a{};
assert(ex::apply(val_fn, a));
assert(ex::apply(cref_fn, a));
}
{
Tuple a{};
assert(ex::apply(val_fn, a));
assert(ex::apply(ref_fn, a));
assert(ex::apply(cref_fn, a));
assert(ex::apply(rref_fn, std::move(a)));
}
{
CTuple a{};
assert(ex::apply(val_fn, a));
assert(ex::apply(cref_fn, a));
}
}
template <std::size_t Size>
void test_one()
{
using A = std::array<int, Size>;
using Tuple = make_tuple_t<int, Size>;
using ValFn = make_function_t<int, Size>;
ValFn val_fn = &test_apply_fn;
{
A a{};
assert(ex::apply(val_fn, a));
}
{
Tuple a{};
assert(ex::apply(val_fn, a));
}
}
int main()
{
// Instantiate with 1-5 arguments.
test_all<1>();
test_all<2>();
test_all<3>();
test_all<4>();
test_all<5>();
// Stress test with 128.
test_one<128>();
//test_one<256>();
}