[v3 PATCH] Reduce the size of variant, it doesn't need an index of type size_t internally.

Jonathan Wakely jwakely@redhat.com
Wed Jan 11 10:29:00 GMT 2017


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.



More information about the Libstdc++ mailing list