PR libstdc++/88119 use alignof in std::alignment_of, not __alignof__
authorJonathan Wakely <jwakely@redhat.com>
Thu, 29 Nov 2018 12:32:57 +0000 (12:32 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 29 Nov 2018 12:32:57 +0000 (12:32 +0000)
Now that __alignof__ and alignof sometimes disagree it matters which one
we use. The standard says that std::alignment_of<T>::value equals
alignof(T), so we need to use that.

Change the only uses of alignment_of to use __alignof__ to avoid a
change in alignment.

PR libstdc++/88119
* include/ext/aligned_buffer.h (__aligned_membuf): Add comment.
(__aligned_buffer): Use __alignof__ instead of std::alignment_of.
* include/std/type_traits (alignment_of): Use alignof instead of
__alignof__.
* testsuite/20_util/alignment_of/value.cc: Fix test to check values
match alignof not __alignof__, as required by the standard.

From-SVN: r266613

libstdc++-v3/ChangeLog
libstdc++-v3/include/ext/aligned_buffer.h
libstdc++-v3/include/std/type_traits
libstdc++-v3/testsuite/20_util/alignment_of/value.cc

index 607edb9beb31eecd5023e98134205fd056be966a..553be512ec585d291534a1a7236ca1a0e5c57b17 100644 (file)
@@ -1,5 +1,13 @@
 2018-11-29  Jonathan Wakely  <jwakely@redhat.com>
 
+       PR libstdc++/88119
+       * include/ext/aligned_buffer.h (__aligned_membuf): Add comment.
+       (__aligned_buffer): Use __alignof__ instead of std::alignment_of.
+       * include/std/type_traits (alignment_of): Use alignof instead of
+       __alignof__.
+       * testsuite/20_util/alignment_of/value.cc: Fix test to check values
+       match alignof not __alignof__, as required by the standard.
+
        PR libstdc++/86910
        PR libstdc++/87846
        * src/filesystem/ops.cc (experimental::create_directories): Report
index 81fb797723c6041cf62cd36f447e11e5e0d49887..2fc8f12e62d6679dbbb5d928eb745dbfd97430fb 100644 (file)
@@ -49,6 +49,8 @@ namespace __gnu_cxx
       // Target macro ADJUST_FIELD_ALIGN can produce different alignment for
       // types when used as class members. __aligned_membuf is intended
       // for use as a class member, so align the buffer as for a class member.
+      // Since GCC 8 we could just use alignof(_Tp) instead, but older
+      // versions of non-GNU compilers might still need this trick.
       struct _Tp2 { _Tp _M_t; };
 
       alignas(__alignof__(_Tp2::_M_t)) unsigned char _M_storage[sizeof(_Tp)];
@@ -86,11 +88,10 @@ namespace __gnu_cxx
   // This type is still used to avoid an ABI change.
   template<typename _Tp>
     struct __aligned_buffer
-    : std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value>
+    : std::aligned_storage<sizeof(_Tp), __alignof__(_Tp)>
     {
       typename
-       std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value>::type
-       _M_storage;
+       std::aligned_storage<sizeof(_Tp), __alignof__(_Tp)>::type _M_storage;
 
       __aligned_buffer() = default;
 
index 60094f9897b5b2b8265847037bec2b77c9a51d18..727a5451c56a2cf2bbbe0f32c965ee7ff0ef0794 100644 (file)
@@ -1286,7 +1286,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   /// alignment_of
   template<typename _Tp>
     struct alignment_of
-    : public integral_constant<std::size_t, __alignof__(_Tp)> { };
+    : public integral_constant<std::size_t, alignof(_Tp)> { };
 
   /// rank
   template<typename>
index 7c46a58bf28f04c73f77fbaea6d851d5f78c506b..f73008daabdcfb021d0c89233502b3ed4e87f5f2 100644 (file)
 #include <type_traits>
 #include <testsuite_tr1.h>
 
-void test01()
+template<typename T>
+constexpr bool test()
 {
-  using std::alignment_of;
-  using namespace __gnu_test;
+  return __gnu_test::test_property<std::alignment_of, T>(alignof(T));
+}
 
-  static_assert(test_property<alignment_of, char>(__alignof__(char)), "");
-  static_assert(test_property<alignment_of, short>(__alignof__(short)), "");
-  static_assert(test_property<alignment_of, int>(__alignof__(int)), "");
-  static_assert(test_property<alignment_of, double>(__alignof__(double)), "");
-  static_assert(test_property<alignment_of, int[4]>(__alignof__(int[4])), "");
-  static_assert(test_property<alignment_of,
-               ClassType>(__alignof__(ClassType)), "");
+void test01()
+{
+  static_assert(test<char>(), "");
+  static_assert(test<short>(), "");
+  static_assert(test<int>(), "");
+  static_assert(test<long>(), "");
+  static_assert(test<long long>(), "");
+  static_assert(test<float>(), "");
+  static_assert(test<double>(), "");
+  static_assert(test<long double>(), "");
+  static_assert(test<int[4]>(), "");
+  static_assert(test<__gnu_test::ClassType>(), "");
 }