diff options
author | Eric Fiselier <eric@efcs.ca> | 2017-11-19 04:19:44 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2017-11-19 04:19:44 +0000 |
commit | e7f8cd476d19d81c210105bc62086b32349e76d9 (patch) | |
tree | 7f394f97823e2ae8ca3ec4aad843f8ed05360909 /test | |
parent | 115071a08e5bb786313b92eeca9afb81d5f43d16 (diff) |
[libc++] Shrink variant's index type when possible
Summary:
Currently `std::variant` always uses an unsigned int to store the variant index. However this isn't nessesary and causes `std::variant` to be larger than it needs to be in most cases.
This patch changes the index type to be `unsigned char` when possible, and `unsigned short` or `unsigned int` otherwise, depending on the size (Although it's questionable if it's even possible to create a variant with 65535 elements.
Unfortunately this change is an ABI break, and as such is only enabled in ABI v2.
Reviewers: mpark
Reviewed By: mpark
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D40210
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@318621 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test')
-rw-r--r-- | test/libcxx/utilities/variant/variant.variant/variant_size.pass.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/test/libcxx/utilities/variant/variant.variant/variant_size.pass.cpp b/test/libcxx/utilities/variant/variant.variant/variant_size.pass.cpp new file mode 100644 index 000000000..a836ef516 --- /dev/null +++ b/test/libcxx/utilities/variant/variant.variant/variant_size.pass.cpp @@ -0,0 +1,68 @@ +// -*- 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, c++14 + +// <variant> + +// template <class ...Types> class variant; + +#include <limits> +#include <type_traits> +#include <utility> +#include <variant> + +template <class Sequence> +struct make_variant_imp; + +template <size_t ...Indices> +struct make_variant_imp<std::integer_sequence<size_t, Indices...>> { + using type = std::variant<decltype((Indices, char(0)))...>; +}; + +template <size_t N> +using make_variant_t = typename make_variant_imp<std::make_index_sequence<N>>::type; + +constexpr bool ExpectEqual = +#ifdef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION + false; +#else + true; +#endif + +template <class IndexType> +void test_index_type() { + using Lim = std::numeric_limits<IndexType>; + using T1 = make_variant_t<Lim::max() - 1>; + using T2 = make_variant_t<Lim::max()>; + static_assert((sizeof(T1) == sizeof(T2)) == ExpectEqual, ""); +} + +template <class IndexType> +void test_index_internals() { + using Lim = std::numeric_limits<IndexType>; + static_assert(std::__choose_index_type(Lim::max() -1) != + std::__choose_index_type(Lim::max()), ""); + static_assert(std::is_same_v< + std::__variant_index_t<Lim::max()-1>, + std::__variant_index_t<Lim::max()> + > == ExpectEqual, ""); + using IndexT = std::__variant_index_t<Lim::max()-1>; + using IndexLim = std::numeric_limits<IndexT>; + static_assert(std::__variant_npos<IndexT> == IndexLim::max(), ""); +} + +int main() { + test_index_type<unsigned char>(); + // This won't compile due to template depth issues. + //test_index_type<unsigned short>(); + test_index_internals<unsigned char>(); + test_index_internals<unsigned short>(); +} |