[libcxx-commits] [libcxx] 0fa118a - Add default initialization to compressed_pair.

Eric Fiselier via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 16 14:14:20 PST 2019


Author: Eric Fiselier
Date: 2019-12-16T17:14:02-05:00
New Revision: 0fa118a9da6786a0aaf81e309d8c3b38bc5f61dd

URL: https://github.com/llvm/llvm-project/commit/0fa118a9da6786a0aaf81e309d8c3b38bc5f61dd
DIFF: https://github.com/llvm/llvm-project/commit/0fa118a9da6786a0aaf81e309d8c3b38bc5f61dd.diff

LOG: Add default initialization to compressed_pair.

This change introduces the __default_init_tag to memory, and a corresponding
element constructor to allow for default initialization of either of the pair
values. This is useful for classes such as std::string where most (all)
constructors explicitly initialize the values in the constructor.

Patch by Martijn Vels (mvels at google.com)
Reviewed as https://reviews.llvm.org/D70617

Added: 
    libcxx/test/libcxx/memory/compressed_pair/compressed_pair.pass.cpp

Modified: 
    libcxx/include/memory

Removed: 
    


################################################################################
diff  --git a/libcxx/include/memory b/libcxx/include/memory
index 1723b30d39d3..45e032ae96fd 100644
--- a/libcxx/include/memory
+++ b/libcxx/include/memory
@@ -2178,6 +2178,9 @@ public:
 };
 #endif
 
+// Tag used to default initialize one or both of the pair's elements.
+struct __default_init_tag {};
+
 template <class _Tp, int _Idx,
           bool _CanBeEmptyBase =
               is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
@@ -2188,6 +2191,8 @@ struct __compressed_pair_elem {
 
 #ifndef _LIBCPP_CXX03_LANG
   _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
+  _LIBCPP_INLINE_VISIBILITY constexpr
+  __compressed_pair_elem(__default_init_tag) {}
 
   template <class _Up, class = typename enable_if<
       !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
@@ -2207,6 +2212,8 @@ struct __compressed_pair_elem {
 #else
   _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
   _LIBCPP_INLINE_VISIBILITY
+  __compressed_pair_elem(__default_init_tag) {}
+  _LIBCPP_INLINE_VISIBILITY
   __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
 #endif
 

diff  --git a/libcxx/test/libcxx/memory/compressed_pair/compressed_pair.pass.cpp b/libcxx/test/libcxx/memory/compressed_pair/compressed_pair.pass.cpp
new file mode 100644
index 000000000000..53acd8face82
--- /dev/null
+++ b/libcxx/test/libcxx/memory/compressed_pair/compressed_pair.pass.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <assert.h>
+#include <memory>
+
+#include "test_macros.h"
+
+typedef std::__compressed_pair<int, unsigned> IntPair;
+
+void test_constructor() {
+  IntPair value;
+  assert(value.first() == 0);
+  assert(value.second() == 0);
+
+  value.first() = 1;
+  value.second() = 2;
+  new (&value) IntPair;
+  assert(value.first() == 0);
+  assert(value.second() == 0);
+}
+
+void test_constructor_default_init() {
+  IntPair value;
+  value.first() = 1;
+  value.second() = 2;
+
+  new (&value) IntPair(std::__default_init_tag(), 3);
+  assert(value.first() == 1);
+  assert(value.second() == 3);
+
+  new (&value) IntPair(4, std::__default_init_tag());
+  assert(value.first() == 4);
+  assert(value.second() == 3);
+
+  new (&value) IntPair(std::__default_init_tag(), std::__default_init_tag());
+  assert(value.first() == 4);
+  assert(value.second() == 3);
+}
+
+int main(int, char**)
+{
+  test_constructor();
+  test_constructor_default_init();
+  return 0;
+}


        


More information about the libcxx-commits mailing list