This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [v3 PATCH] Reduce the size of variant, it doesn't need an index of type size_t internally.
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: Ville Voutilainen <ville dot voutilainen at gmail dot com>
- Cc: "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>, libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Wed, 11 Jan 2017 10:29:07 +0000
- Subject: Re: [v3 PATCH] Reduce the size of variant, it doesn't need an index of type size_t internally.
- Authentication-results: sourceware.org; auth=none
- References: <CAFk2RUZvtch1=XANuPeomv+vfL_5tt8ZXWA6jr_tfXwgY03EHA@mail.gmail.com> <CAFk2RUZD3r6nbWKJ-T8MUQnbFSxp6HtFXaaJDCvcg5tK+UqJrA@mail.gmail.com>
On 11/01/17 00:19 +0200, Ville Voutilainen wrote:
@@ -1086,7 +1099,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ return !this->_M_valid(); }
constexpr size_t index() const noexcept
- { return this->_M_index; }
+ {
+ if (this->_M_index ==
+ typename _Base::_Storage::__index_type(variant_npos))
+ return variant_npos;
+ return this->_M_index;
GCC doesn't seem to be smart enough to optimize the branch away here.
Something like this would avoid it:
constexpr size_t index const noexcept
{
using __index_type = typename _Base::_Storage::__index_type;
return size_t(__index_type(this->_M_index + 1)) - 1;
}
But we can worry about that later.
+ }
void
swap(variant& __rhs)
diff --git a/libstdc++-v3/testsuite/20_util/variant/index_type.cc b/libstdc++-v3/testsuite/20_util/variant/index_type.cc
new file mode 100644
index 0000000..b7f3a7b
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/variant/index_type.cc
@@ -0,0 +1,24 @@
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target x86_64-*-* powerpc*-*-*} }
Please add a space after the second target (it seems to work anyway,
but still better to add it).
OK for trunk with that space character added, thanks.