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.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@202994 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2014-03-05 19:06:20 +00:00
parent 24b29a02f1
commit 48c74700ec
17 changed files with 91 additions and 8 deletions

View File

@ -1208,7 +1208,7 @@ public:
deque() deque()
_NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) _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); explicit deque(size_type __n);
#if _LIBCPP_STD_VER > 11 #if _LIBCPP_STD_VER > 11
explicit deque(size_type __n, const _Allocator& __a); explicit deque(size_type __n, const _Allocator& __a);

View File

@ -835,7 +835,7 @@ public:
typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator; typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY
explicit map(const key_compare& __comp = key_compare()) map(const key_compare& __comp = key_compare())
_NOEXCEPT_( _NOEXCEPT_(
is_nothrow_default_constructible<allocator_type>::value && is_nothrow_default_constructible<allocator_type>::value &&
is_nothrow_default_constructible<key_compare>::value && is_nothrow_default_constructible<key_compare>::value &&
@ -1568,7 +1568,7 @@ public:
typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator; typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY
explicit multimap(const key_compare& __comp = key_compare()) multimap(const key_compare& __comp = key_compare())
_NOEXCEPT_( _NOEXCEPT_(
is_nothrow_default_constructible<allocator_type>::value && is_nothrow_default_constructible<allocator_type>::value &&
is_nothrow_default_constructible<key_compare>::value && is_nothrow_default_constructible<key_compare>::value &&

View File

@ -425,14 +425,14 @@ public:
typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator; typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY
explicit set(const value_compare& __comp = value_compare()) set(const value_compare& __comp = value_compare())
_NOEXCEPT_( _NOEXCEPT_(
is_nothrow_default_constructible<allocator_type>::value && is_nothrow_default_constructible<allocator_type>::value &&
is_nothrow_default_constructible<key_compare>::value && is_nothrow_default_constructible<key_compare>::value &&
is_nothrow_copy_constructible<key_compare>::value) is_nothrow_copy_constructible<key_compare>::value)
: __tree_(__comp) {} : __tree_(__comp) {}
_LIBCPP_INLINE_VISIBILITY _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) {} : __tree_(__comp, __a) {}
template <class _InputIterator> template <class _InputIterator>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY
@ -822,14 +822,14 @@ public:
// construct/copy/destroy: // construct/copy/destroy:
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY
explicit multiset(const value_compare& __comp = value_compare()) multiset(const value_compare& __comp = value_compare())
_NOEXCEPT_( _NOEXCEPT_(
is_nothrow_default_constructible<allocator_type>::value && is_nothrow_default_constructible<allocator_type>::value &&
is_nothrow_default_constructible<key_compare>::value && is_nothrow_default_constructible<key_compare>::value &&
is_nothrow_copy_constructible<key_compare>::value) is_nothrow_copy_constructible<key_compare>::value)
: __tree_(__comp) {} : __tree_(__comp) {}
_LIBCPP_INLINE_VISIBILITY _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) {} : __tree_(__comp, __a) {}
template <class _InputIterator> template <class _InputIterator>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY

View File

@ -31,5 +31,10 @@ int main()
assert(m.empty()); assert(m.empty());
assert(m.begin() == m.end()); assert(m.begin() == m.end());
} }
{
std::map<int, double> m = {};
assert(m.empty());
assert(m.begin() == m.end());
}
#endif #endif
} }

View File

@ -31,5 +31,10 @@ int main()
assert(m.empty()); assert(m.empty());
assert(m.begin() == m.end()); assert(m.begin() == m.end());
} }
{
std::multimap<int, double> m = {};
assert(m.empty());
assert(m.begin() == m.end());
}
#endif #endif
} }

View File

@ -31,5 +31,10 @@ int main()
assert(m.empty()); assert(m.empty());
assert(m.begin() == m.end()); assert(m.begin() == m.end());
} }
{
std::multiset<int> m = {};
assert(m.empty());
assert(m.begin() == m.end());
}
#endif #endif
} }

View File

@ -31,5 +31,10 @@ int main()
assert(m.empty()); assert(m.empty());
assert(m.begin() == m.end()); assert(m.begin() == m.end());
} }
{
std::set<int> m = {};
assert(m.empty());
assert(m.begin() == m.end());
}
#endif #endif
} }

View File

@ -24,6 +24,10 @@ test()
{ {
std::deque<T, Allocator> d; std::deque<T, Allocator> d;
assert(d.size() == 0); assert(d.size() == 0);
#if __cplusplus >= 201103L
std::deque<T, Allocator> d1 = {};
assert(d1.size() == 0);
#endif
} }
int main() int main()

View File

@ -31,5 +31,11 @@ int main()
C c; C c;
assert(c.empty()); assert(c.empty());
} }
{
typedef int T;
typedef std::forward_list<T> C;
C c = {};
assert(c.empty());
}
#endif #endif
} }

View File

@ -49,5 +49,10 @@ int main()
assert(l.size() == 0); assert(l.size() == 0);
assert(std::distance(l.begin(), l.end()) == 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 #endif
} }

View File

@ -26,6 +26,12 @@ test0()
assert(c.__invariants()); assert(c.__invariants());
assert(c.empty()); assert(c.empty());
assert(c.get_allocator() == typename C::allocator_type()); 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> template <class C>

View File

@ -27,6 +27,12 @@ test0()
assert(c.__invariants()); assert(c.__invariants());
assert(c.empty()); assert(c.empty());
assert(c.get_allocator() == typename C::allocator_type()); 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> template <class C>

View File

@ -86,7 +86,7 @@ int main()
}; };
C c(a, a + sizeof(a)/sizeof(a[0])); C c(a, a + sizeof(a)/sizeof(a[0]));
C *p = &c; C *p = &c;
c = *p; c = *p;
assert(c.size() == 4); assert(c.size() == 4);
assert(std::is_permutation(c.begin(), c.end(), a)); assert(std::is_permutation(c.begin(), c.end(), a));
} }

View File

@ -65,5 +65,14 @@ int main()
assert(c.load_factor() == 0); assert(c.load_factor() == 0);
assert(c.max_load_factor() == 1); 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 #endif
} }

View File

@ -65,5 +65,14 @@ int main()
assert(c.load_factor() == 0); assert(c.load_factor() == 0);
assert(c.max_load_factor() == 1); 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 #endif
} }

View File

@ -61,5 +61,14 @@ int main()
assert(c.load_factor() == 0); assert(c.load_factor() == 0);
assert(c.max_load_factor() == 1); 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 #endif
} }

View File

@ -61,5 +61,14 @@ int main()
assert(c.load_factor() == 0); assert(c.load_factor() == 0);
assert(c.max_load_factor() == 1); 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 #endif
} }