[libcxx] r202994 - Implement LWG 2193. Default constructors for standard library containers are explicit. Note that libc++ already did this for string/deque/forward_list/list/vector and the unordered containers; implement it for set/multiset/map/multimap. Add tests for all the containers. Two drive-by fixes as well: add a missing explicit in <deque>, and remove a tab that snuck into a container test. This issue is also LLVM bug 15724, and resolves it.

Marshall Clow mclow.lists at gmail.com
Wed Mar 5 11:06:20 PST 2014


Author: marshall
Date: Wed Mar  5 13:06:20 2014
New Revision: 202994

URL: http://llvm.org/viewvc/llvm-project?rev=202994&view=rev
Log:
Implement LWG 2193. Default constructors for standard library containers are explicit. Note that libc++ already did this for string/deque/forward_list/list/vector and the unordered containers; implement it for set/multiset/map/multimap. Add tests for all the containers. Two drive-by fixes as well: add a missing explicit in <deque>, and remove a tab that snuck into a container test. This issue is also LLVM bug 15724, and resolves it.

Modified:
    libcxx/trunk/include/deque
    libcxx/trunk/include/map
    libcxx/trunk/include/set
    libcxx/trunk/test/containers/associative/map/map.cons/default.pass.cpp
    libcxx/trunk/test/containers/associative/multimap/multimap.cons/default.pass.cpp
    libcxx/trunk/test/containers/associative/multiset/multiset.cons/default.pass.cpp
    libcxx/trunk/test/containers/associative/set/set.cons/default.pass.cpp
    libcxx/trunk/test/containers/sequences/deque/deque.cons/default.pass.cpp
    libcxx/trunk/test/containers/sequences/forwardlist/forwardlist.cons/default.pass.cpp
    libcxx/trunk/test/containers/sequences/list/list.cons/default.pass.cpp
    libcxx/trunk/test/containers/sequences/vector.bool/construct_default.pass.cpp
    libcxx/trunk/test/containers/sequences/vector/vector.cons/construct_default.pass.cpp
    libcxx/trunk/test/containers/unord/unord.map/unord.map.cnstr/assign_copy.pass.cpp
    libcxx/trunk/test/containers/unord/unord.map/unord.map.cnstr/default.pass.cpp
    libcxx/trunk/test/containers/unord/unord.multimap/unord.multimap.cnstr/default.pass.cpp
    libcxx/trunk/test/containers/unord/unord.multiset/unord.multiset.cnstr/default.pass.cpp
    libcxx/trunk/test/containers/unord/unord.set/unord.set.cnstr/default.pass.cpp

Modified: libcxx/trunk/include/deque
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/deque?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/include/deque (original)
+++ libcxx/trunk/include/deque Wed Mar  5 13:06:20 2014
@@ -1208,7 +1208,7 @@ public:
     deque()
         _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
         {}
-    _LIBCPP_INLINE_VISIBILITY deque(const allocator_type& __a) : __base(__a) {}
+    _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
     explicit deque(size_type __n);
 #if _LIBCPP_STD_VER > 11
     explicit deque(size_type __n, const _Allocator& __a);

Modified: libcxx/trunk/include/map
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/map?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/include/map (original)
+++ libcxx/trunk/include/map Wed Mar  5 13:06:20 2014
@@ -835,7 +835,7 @@ public:
     typedef _VSTD::reverse_iterator<const_iterator>         const_reverse_iterator;
 
     _LIBCPP_INLINE_VISIBILITY
-    explicit map(const key_compare& __comp = key_compare())
+    map(const key_compare& __comp = key_compare())
         _NOEXCEPT_(
             is_nothrow_default_constructible<allocator_type>::value &&
             is_nothrow_default_constructible<key_compare>::value &&
@@ -1568,7 +1568,7 @@ public:
     typedef _VSTD::reverse_iterator<const_iterator>         const_reverse_iterator;
 
     _LIBCPP_INLINE_VISIBILITY
-    explicit multimap(const key_compare& __comp = key_compare())
+    multimap(const key_compare& __comp = key_compare())
         _NOEXCEPT_(
             is_nothrow_default_constructible<allocator_type>::value &&
             is_nothrow_default_constructible<key_compare>::value &&

Modified: libcxx/trunk/include/set
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/set?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/include/set (original)
+++ libcxx/trunk/include/set Wed Mar  5 13:06:20 2014
@@ -425,14 +425,14 @@ public:
     typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
 
     _LIBCPP_INLINE_VISIBILITY
-    explicit set(const value_compare& __comp = value_compare())
+    set(const value_compare& __comp = value_compare())
         _NOEXCEPT_(
             is_nothrow_default_constructible<allocator_type>::value &&
             is_nothrow_default_constructible<key_compare>::value &&
             is_nothrow_copy_constructible<key_compare>::value)
         : __tree_(__comp) {}
     _LIBCPP_INLINE_VISIBILITY
-    set(const value_compare& __comp, const allocator_type& __a)
+    explicit set(const value_compare& __comp, const allocator_type& __a)
         : __tree_(__comp, __a) {}
     template <class _InputIterator>
         _LIBCPP_INLINE_VISIBILITY
@@ -822,14 +822,14 @@ public:
 
     // construct/copy/destroy:
     _LIBCPP_INLINE_VISIBILITY
-    explicit multiset(const value_compare& __comp = value_compare())
+    multiset(const value_compare& __comp = value_compare())
         _NOEXCEPT_(
             is_nothrow_default_constructible<allocator_type>::value &&
             is_nothrow_default_constructible<key_compare>::value &&
             is_nothrow_copy_constructible<key_compare>::value)
         : __tree_(__comp) {}
     _LIBCPP_INLINE_VISIBILITY
-    multiset(const value_compare& __comp, const allocator_type& __a)
+    explicit multiset(const value_compare& __comp, const allocator_type& __a)
         : __tree_(__comp, __a) {}
     template <class _InputIterator>
         _LIBCPP_INLINE_VISIBILITY

Modified: libcxx/trunk/test/containers/associative/map/map.cons/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/associative/map/map.cons/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/associative/map/map.cons/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/associative/map/map.cons/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -31,5 +31,10 @@ int main()
     assert(m.empty());
     assert(m.begin() == m.end());
     }
+    {
+    std::map<int, double> m = {};
+    assert(m.empty());
+    assert(m.begin() == m.end());
+    }
 #endif
 }

Modified: libcxx/trunk/test/containers/associative/multimap/multimap.cons/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/associative/multimap/multimap.cons/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/associative/multimap/multimap.cons/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/associative/multimap/multimap.cons/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -31,5 +31,10 @@ int main()
     assert(m.empty());
     assert(m.begin() == m.end());
     }
+    {
+    std::multimap<int, double> m = {};
+    assert(m.empty());
+    assert(m.begin() == m.end());
+    }
 #endif
 }

Modified: libcxx/trunk/test/containers/associative/multiset/multiset.cons/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/associative/multiset/multiset.cons/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/associative/multiset/multiset.cons/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/associative/multiset/multiset.cons/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -31,5 +31,10 @@ int main()
     assert(m.empty());
     assert(m.begin() == m.end());
     }
+    {
+    std::multiset<int> m = {};
+    assert(m.empty());
+    assert(m.begin() == m.end());
+    }
 #endif
 }

Modified: libcxx/trunk/test/containers/associative/set/set.cons/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/associative/set/set.cons/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/associative/set/set.cons/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/associative/set/set.cons/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -31,5 +31,10 @@ int main()
     assert(m.empty());
     assert(m.begin() == m.end());
     }
+    {
+    std::set<int> m = {};
+    assert(m.empty());
+    assert(m.begin() == m.end());
+    }
 #endif
 }

Modified: libcxx/trunk/test/containers/sequences/deque/deque.cons/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/deque/deque.cons/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/sequences/deque/deque.cons/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/sequences/deque/deque.cons/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -24,6 +24,10 @@ test()
 {
     std::deque<T, Allocator> d;
     assert(d.size() == 0);
+#if __cplusplus >= 201103L
+    std::deque<T, Allocator> d1 = {};
+    assert(d1.size() == 0);
+#endif
 }
 
 int main()

Modified: libcxx/trunk/test/containers/sequences/forwardlist/forwardlist.cons/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/forwardlist/forwardlist.cons/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/sequences/forwardlist/forwardlist.cons/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/sequences/forwardlist/forwardlist.cons/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -31,5 +31,11 @@ int main()
         C c;
         assert(c.empty());
     }
+    {
+        typedef int T;
+        typedef std::forward_list<T> C;
+        C c = {};
+        assert(c.empty());
+    }
 #endif
 }

Modified: libcxx/trunk/test/containers/sequences/list/list.cons/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/list/list.cons/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/sequences/list/list.cons/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/sequences/list/list.cons/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -49,5 +49,10 @@ int main()
         assert(l.size() == 0);
         assert(std::distance(l.begin(), l.end()) == 0);
     }
+    {
+        std::list<int> l = {};
+        assert(l.size() == 0);
+        assert(std::distance(l.begin(), l.end()) == 0);
+    }
 #endif
 }

Modified: libcxx/trunk/test/containers/sequences/vector.bool/construct_default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector.bool/construct_default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector.bool/construct_default.pass.cpp (original)
+++ libcxx/trunk/test/containers/sequences/vector.bool/construct_default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -26,6 +26,12 @@ test0()
     assert(c.__invariants());
     assert(c.empty());
     assert(c.get_allocator() == typename C::allocator_type());
+#if __cplusplus >= 201103L
+    C c1 = {};
+    assert(c1.__invariants());
+    assert(c1.empty());
+    assert(c1.get_allocator() == typename C::allocator_type());
+#endif
 }
 
 template <class C>

Modified: libcxx/trunk/test/containers/sequences/vector/vector.cons/construct_default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/vector.cons/construct_default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/vector.cons/construct_default.pass.cpp (original)
+++ libcxx/trunk/test/containers/sequences/vector/vector.cons/construct_default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -27,6 +27,12 @@ test0()
     assert(c.__invariants());
     assert(c.empty());
     assert(c.get_allocator() == typename C::allocator_type());
+#if __cplusplus >= 201103L
+    C c1 = {};
+    assert(c1.__invariants());
+    assert(c1.empty());
+    assert(c1.get_allocator() == typename C::allocator_type());
+#endif
 }
 
 template <class C>

Modified: libcxx/trunk/test/containers/unord/unord.map/unord.map.cnstr/assign_copy.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/unord/unord.map/unord.map.cnstr/assign_copy.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/unord/unord.map/unord.map.cnstr/assign_copy.pass.cpp (original)
+++ libcxx/trunk/test/containers/unord/unord.map/unord.map.cnstr/assign_copy.pass.cpp Wed Mar  5 13:06:20 2014
@@ -86,7 +86,7 @@ int main()
         };
         C c(a, a + sizeof(a)/sizeof(a[0]));
         C *p = &c;
-		c = *p;
+        c = *p;
         assert(c.size() == 4);
         assert(std::is_permutation(c.begin(), c.end(), a));
     }

Modified: libcxx/trunk/test/containers/unord/unord.map/unord.map.cnstr/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/unord/unord.map/unord.map.cnstr/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/unord/unord.map/unord.map.cnstr/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/unord/unord.map/unord.map.cnstr/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -65,5 +65,14 @@ int main()
         assert(c.load_factor() == 0);
         assert(c.max_load_factor() == 1);
     }
+    {
+        std::unordered_map<int, int> c = {};
+        assert(c.bucket_count() == 0);
+        assert(c.size() == 0);
+        assert(c.empty());
+        assert(std::distance(c.begin(), c.end()) == 0);
+        assert(c.load_factor() == 0);
+        assert(c.max_load_factor() == 1);
+    }
 #endif
 }

Modified: libcxx/trunk/test/containers/unord/unord.multimap/unord.multimap.cnstr/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/unord/unord.multimap/unord.multimap.cnstr/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/unord/unord.multimap/unord.multimap.cnstr/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/unord/unord.multimap/unord.multimap.cnstr/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -65,5 +65,14 @@ int main()
         assert(c.load_factor() == 0);
         assert(c.max_load_factor() == 1);
     }
+    {
+        std::unordered_multimap<int, int> c = {};
+        assert(c.bucket_count() == 0);
+        assert(c.size() == 0);
+        assert(c.empty());
+        assert(std::distance(c.begin(), c.end()) == 0);
+        assert(c.load_factor() == 0);
+        assert(c.max_load_factor() == 1);
+    }
 #endif
 }

Modified: libcxx/trunk/test/containers/unord/unord.multiset/unord.multiset.cnstr/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/unord/unord.multiset/unord.multiset.cnstr/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/unord/unord.multiset/unord.multiset.cnstr/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/unord/unord.multiset/unord.multiset.cnstr/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -61,5 +61,14 @@ int main()
         assert(c.load_factor() == 0);
         assert(c.max_load_factor() == 1);
     }
+    {
+        std::unordered_multiset<int> c = {};
+        assert(c.bucket_count() == 0);
+        assert(c.size() == 0);
+        assert(c.empty());
+        assert(std::distance(c.begin(), c.end()) == 0);
+        assert(c.load_factor() == 0);
+        assert(c.max_load_factor() == 1);
+    }
 #endif
 }

Modified: libcxx/trunk/test/containers/unord/unord.set/unord.set.cnstr/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/unord/unord.set/unord.set.cnstr/default.pass.cpp?rev=202994&r1=202993&r2=202994&view=diff
==============================================================================
--- libcxx/trunk/test/containers/unord/unord.set/unord.set.cnstr/default.pass.cpp (original)
+++ libcxx/trunk/test/containers/unord/unord.set/unord.set.cnstr/default.pass.cpp Wed Mar  5 13:06:20 2014
@@ -61,5 +61,14 @@ int main()
         assert(c.load_factor() == 0);
         assert(c.max_load_factor() == 1);
     }
+    {
+        std::unordered_set<int> c = {};
+        assert(c.bucket_count() == 0);
+        assert(c.size() == 0);
+        assert(c.empty());
+        assert(std::distance(c.begin(), c.end()) == 0);
+        assert(c.load_factor() == 0);
+        assert(c.max_load_factor() == 1);
+    }
 #endif
 }





More information about the cfe-commits mailing list