On 31/10/18 12:31 +0000, Jonathan Wakely wrote:
The introduction of the empty __pair_base base class for PR 86751
changed the layout of std::pair<std::pair<...>, ...>. The outer pair and
its first member both have a base class of the same type, which cannot
exist at the same address. This causes the first member to be at a
non-zero offset.

The solution is to make the base class depend on the template
parameters, so that each pair type has a different base class type,
which allows the base classes of the outer pair and its first member to
have the same address.

        PR libstdc++/87822
        * include/bits/stl_pair.h (__pair_base): Change to class template.
        (pair): Make base class type depend on template parameters.
        * testsuite/20_util/pair/87822.cc: New test.

Jakub suggested some more testing, as attached.

Tested powerpc64le-linux and x86_64-linux, committed to trunk.
I'll include this in the backports too.


commit 75efd61131798bd9001b15b551bd6f1cbfc2bc33
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Wed Oct 31 12:57:07 2018 +0000

    More testing for std::pair layout change
    
            * testsuite/20_util/pair/87822.cc: Test deeper nesting.

diff --git a/libstdc++-v3/testsuite/20_util/pair/87822.cc b/libstdc++-v3/testsuite/20_util/pair/87822.cc
index cd099d6f9f9..523d583f08a 100644
--- a/libstdc++-v3/testsuite/20_util/pair/87822.cc
+++ b/libstdc++-v3/testsuite/20_util/pair/87822.cc
@@ -26,6 +26,7 @@ test01()
   static_assert(sizeof(p) == (3 * sizeof(int)), "PR libstdc++/87822");
 #endif
   VERIFY( (void*)&p == (void*)&p.first );
+  VERIFY( (void*)&p == (void*)&p.first.first );
 }
 
 struct empty { };
@@ -40,8 +41,24 @@ test02()
   VERIFY( (void*)&p == (void*)&p.first );
 }
 
+void
+test03()
+{
+  typedef std::pair<int, int> int_pair;
+  typedef std::pair<int_pair, int_pair> int_pair_pair;
+  std::pair<int_pair_pair, int_pair_pair> p;
+#if __cplusplus >= 201103L
+  static_assert(sizeof(int_pair_pair) == (2 * sizeof(int_pair)), "nested");
+  static_assert(sizeof(p) == (2 * sizeof(int_pair_pair)), "nested again");
+#endif
+  VERIFY( (void*)&p == (void*)&p.first );
+  VERIFY( (void*)&p == (void*)&p.first.first );
+  VERIFY( (void*)&p == (void*)&p.first.first.first );
+}
+
 int main()
 {
   test01();
   test02();
+  test03();
 }

Reply via email to