More of N4258 implementation. Mark all of our test_allocators as noexcept constructible. Make the constructors for basic_string noexcept all the time (under C++14). Update tests to reflect the new world order. More to come.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@238957 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2015-06-03 19:56:43 +00:00
parent bbf87b1c34
commit 7b193f7797
8 changed files with 113 additions and 29 deletions

View File

@ -1321,14 +1321,31 @@ public:
static const size_type npos = -1;
_LIBCPP_INLINE_VISIBILITY basic_string()
#if _LIBCPP_STD_VER <= 14
_NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
_LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a);
#else
_NOEXCEPT;
#endif
_LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
#if _LIBCPP_STD_VER <= 14
_NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
#else
_NOEXCEPT;
#endif
basic_string(const basic_string& __str);
basic_string(const basic_string& __str, const allocator_type& __a);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
_LIBCPP_INLINE_VISIBILITY
basic_string(basic_string&& __str)
#if _LIBCPP_STD_VER <= 14
_NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
#else
_NOEXCEPT;
#endif
_LIBCPP_INLINE_VISIBILITY
basic_string(basic_string&& __str, const allocator_type& __a);
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
@ -1926,7 +1943,11 @@ basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_INLINE_VISIBILITY
basic_string<_CharT, _Traits, _Allocator>::basic_string()
_NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
#if _LIBCPP_STD_VER <= 14
_NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
#else
_NOEXCEPT
#endif
{
#if _LIBCPP_DEBUG_LEVEL >= 2
__get_db()->__insert_c(this);
@ -2070,7 +2091,11 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __st
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_INLINE_VISIBILITY
basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
#if _LIBCPP_STD_VER <= 14
_NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
#else
_NOEXCEPT
#endif
: __r_(_VSTD::move(__str.__r_))
{
__str.__zero();

View File

@ -14,6 +14,7 @@
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
@ -22,6 +23,11 @@ void
test()
{
{
#if TEST_STD_VER > 14
static_assert((noexcept(S{})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" );
#endif
S s;
assert(s.__invariants());
assert(s.data());
@ -30,6 +36,11 @@ test()
assert(s.get_allocator() == typename S::allocator_type());
}
{
#if TEST_STD_VER > 14
static_assert((noexcept(S{typename S::allocator_type{}})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible<typename S::allocator_type>::value), "" );
#endif
S s(typename S::allocator_type(5));
assert(s.__invariants());
assert(s.data());
@ -39,13 +50,18 @@ test()
}
}
#if __cplusplus >= 201103L
#if TEST_STD_VER >= 11
template <class S>
void
test2()
{
{
#if TEST_STD_VER > 14
static_assert((noexcept(S{})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" );
#endif
S s;
assert(s.__invariants());
assert(s.data());
@ -54,6 +70,11 @@ test2()
assert(s.get_allocator() == typename S::allocator_type());
}
{
#if TEST_STD_VER > 14
static_assert((noexcept(S{typename S::allocator_type{}})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible<typename S::allocator_type>::value), "" );
#endif
S s(typename S::allocator_type{});
assert(s.__invariants());
assert(s.data());
@ -68,7 +89,7 @@ test2()
int main()
{
test<std::basic_string<char, std::char_traits<char>, test_allocator<char> > >();
#if __cplusplus >= 201103L
#if TEST_STD_VER >= 11
test2<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
#endif
}

View File

@ -17,6 +17,7 @@
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
template <class T>
@ -39,7 +40,12 @@ int main()
}
{
typedef std::basic_string<char, std::char_traits<char>, some_alloc<char>> C;
// See N4258 - basic_string<char, traits, Allocator>::basic_string() noexcept;
#if TEST_STD_VER <= 14
static_assert(!std::is_nothrow_default_constructible<C>::value, "");
#else
static_assert( std::is_nothrow_default_constructible<C>::value, "");
#endif
}
#endif
}

View File

@ -16,6 +16,7 @@
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
@ -34,6 +35,11 @@ test(S s0, const typename S::allocator_type& a)
}
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
// #if _LIBCPP_STD_VER <= 14
// _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
// #else
// _NOEXCEPT;
// #endif
int main()
{
@ -41,6 +47,11 @@ int main()
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
#if TEST_STD_VER > 14
static_assert((noexcept(S{})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
#endif
test(S(), A(3));
test(S("1"), A(5));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7));
@ -50,15 +61,25 @@ int main()
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
#if TEST_STD_VER > 14
static_assert((noexcept(S{})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
#endif
S s1 ( "Twas brillig, and the slivy toves did gyre and gymbal in the wabe" );
S s2 (std::move(s1), A(1));
}
assert ( test_alloc_base::alloc_count == alloc_count );
#if __cplusplus >= 201103L
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
#if TEST_STD_VER > 14
static_assert((noexcept(S{})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
#endif
test(S(), A());
test(S("1"), A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());

View File

@ -17,6 +17,7 @@
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
template <class T>
@ -39,7 +40,11 @@ int main()
}
{
typedef std::basic_string<char, std::char_traits<char>, some_alloc<char>> C;
#if TEST_STD_VER <= 14
static_assert(!std::is_nothrow_move_constructible<C>::value, "");
#else
static_assert( std::is_nothrow_move_constructible<C>::value, "");
#endif
}
#endif
}

View File

@ -13,6 +13,8 @@
#include <type_traits>
#include <utility>
#include "test_macros.h"
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
template <class T>
@ -20,7 +22,7 @@ class A1
{
int id_;
public:
explicit A1(int id = 0) : id_(id) {}
explicit A1(int id = 0) TEST_NOEXCEPT : id_(id) {}
typedef T value_type;
@ -31,13 +33,13 @@ public:
static bool allocate_called;
static std::pair<T*, std::size_t> deallocate_called;
A1(const A1& a) : id_(a.id()) {copy_called = true;}
A1(A1&& a) : id_(a.id()) {move_called = true;}
A1(const A1& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
A1(A1&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
template <class U>
A1(const A1<U>& a) : id_(a.id()) {copy_called = true;}
A1(const A1<U>& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
template <class U>
A1(A1<U>&& a) : id_(a.id()) {move_called = true;}
A1(A1<U>&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
T* allocate(std::size_t n)
{
@ -77,7 +79,7 @@ class A2
{
int id_;
public:
explicit A2(int id = 0) : id_(id) {}
explicit A2(int id = 0) TEST_NOEXCEPT : id_(id) {}
typedef T value_type;
@ -92,8 +94,8 @@ public:
static bool move_called;
static bool allocate_called;
A2(const A2& a) : id_(a.id()) {copy_called = true;}
A2(A2&& a) : id_(a.id()) {move_called = true;}
A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
A2(A2&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
T* allocate(std::size_t n, const void* hint)
{
@ -125,7 +127,7 @@ class A3
{
int id_;
public:
explicit A3(int id = 0) : id_(id) {}
explicit A3(int id = 0) TEST_NOEXCEPT : id_(id) {}
typedef T value_type;
@ -139,8 +141,8 @@ public:
static bool constructed;
static bool destroy_called;
A3(const A3& a) : id_(a.id()) {copy_called = true;}
A3(A3&& a) : id_(a.id()) {move_called = true;}
A3(const A3& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
A3(A3&& a) TEST_NOEXCEPT: id_(a.id()) {move_called = true;}
template <class U, class ...Args>
void construct(U* p, Args&& ...args)

View File

@ -12,16 +12,18 @@
#include <cstddef>
#include "test_macros.h"
template <class T>
class bare_allocator
{
public:
typedef T value_type;
bare_allocator() {}
bare_allocator() TEST_NOEXCEPT {}
template <class U>
bare_allocator(bare_allocator<U>) {}
bare_allocator(bare_allocator<U>) TEST_NOEXCEPT {}
T* allocate(std::size_t n)
{
@ -53,10 +55,10 @@ class min_pointer<const void>
{
const void* ptr_;
public:
min_pointer() noexcept = default;
min_pointer(std::nullptr_t) : ptr_(nullptr) {}
min_pointer() TEST_NOEXCEPT = default;
min_pointer(std::nullptr_t) TEST_NOEXCEPT : ptr_(nullptr) {}
template <class T>
min_pointer(min_pointer<T> p) : ptr_(p.ptr_) {}
min_pointer(min_pointer<T> p) TEST_NOEXCEPT : ptr_(p.ptr_) {}
explicit operator bool() const {return ptr_ != nullptr;}
@ -70,15 +72,15 @@ class min_pointer<void>
{
void* ptr_;
public:
min_pointer() noexcept = default;
min_pointer(std::nullptr_t) : ptr_(nullptr) {}
min_pointer() TEST_NOEXCEPT = default;
min_pointer(std::nullptr_t) TEST_NOEXCEPT : ptr_(nullptr) {}
template <class T,
class = typename std::enable_if
<
!std::is_const<T>::value
>::type
>
min_pointer(min_pointer<T> p) : ptr_(p.ptr_) {}
min_pointer(min_pointer<T> p) TEST_NOEXCEPT : ptr_(p.ptr_) {}
explicit operator bool() const {return ptr_ != nullptr;}
@ -92,11 +94,11 @@ class min_pointer
{
T* ptr_;
explicit min_pointer(T* p) : ptr_(p) {}
explicit min_pointer(T* p) TEST_NOEXCEPT : ptr_(p) {}
public:
min_pointer() noexcept = default;
min_pointer(std::nullptr_t) : ptr_(nullptr) {}
explicit min_pointer(min_pointer<void> p) : ptr_(static_cast<T*>(p.ptr_)) {}
min_pointer() TEST_NOEXCEPT = default;
min_pointer(std::nullptr_t) TEST_NOEXCEPT : ptr_(nullptr) {}
explicit min_pointer(min_pointer<void> p) TEST_NOEXCEPT : ptr_(static_cast<T*>(p.ptr_)) {}
explicit operator bool() const {return ptr_ != nullptr;}
@ -164,7 +166,7 @@ class min_pointer<const T>
explicit min_pointer(const T* p) : ptr_(p) {}
public:
min_pointer() noexcept = default;
min_pointer() TEST_NOEXCEPT = default;
min_pointer(std::nullptr_t) : ptr_(nullptr) {}
min_pointer(min_pointer<T> p) : ptr_(p.ptr_) {}
explicit min_pointer(min_pointer<const void> p) : ptr_(static_cast<const T*>(p.ptr_)) {}

View File

@ -17,6 +17,8 @@
#include <climits>
#include <cassert>
#include "test_macros.h"
class test_alloc_base
{
protected: